0

I wish to load a json file using extjs. Previously I was loading my json file with the help of jquery library as below:

Ext.define('app.store.Students', {
    extend: 'Ext.data.Store',
    model: 'app.model.Student',
    storeId: 'StudentData',
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url: 'data/users.json',
        success: function(resp) {
        console.log(resp.responseText);
        },
        reader: {
            type: 'json',
            root: 'student1',
            model: 'app.model.Student',
            successProperty: 'success'
        }
    }
});

But now I want to load my json file using ExtJs. I tried the below code:

Ext.define('app.store.Students', {
    extend: 'Ext.data.Store',
    model: 'app.model.Student',
    storeId: 'StudentData',
    autoLoad: true,
});
Ext.Ajax.request({
    url: 'data/users.json',
    //method: 'GET',
    success: function(response){
        var text = response.responseText;
        alert('1')
        console.log(this.url);
        // process server response here
    }
});

but it says 'Uncaught TypeError: Cannot call method 'indexOf' of undefined'. When inspected in firebug, the url given below is undefined in ext-all-debug.js.

    urlAppend : function(url, string) {
    if (!Ext.isEmpty(string)) {
    return url + (url.indexOf('?') === -1 ? '?' : '&') + string;
    }
    return url;
    }, 

I tried to console the url as given above in Ext.Ajax.request method, it says url is undefined. Where am I going wrong?

FYI: This is my file structure:

-html file
-data folder - users.json
-app folder - store folder - js file (This is where I use my Ext.Ajax.request method)

Am I going wrong in the relative path? If so, how I should use it?

1 Answer 1

3

If data is not specified, and if autoLoad is true or an Object, this store's load method is automatically called after creation.Either set autoLoad to false or use proxy with type ajax and reader and then a callback function to handle your json data.

Update : Try this

Ext.define('app.store.Students', {
extend: 'Ext.data.Store',
model: 'app.model.Student',
storeId: 'StudentData',
autoLoad: true,
proxy: {
    type: 'ajax',
    url: 'app/data/users.json',
    reader:{
        type: 'json',
        root: 'student1'//If you are using student1 as root.
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried setting autoLoad to false. I dont get the error 'Uncaught TypeError: Cannot call method 'indexOf' of undefined' anymore. But, I still cant get the datas loaded in my page. I mean the url is wrong. I'm using my relative path : ' url: 'data/users.json','. Is this wrong?
structure should be : app folder->stores, data folder, models, views I mean all the above should be in your app folder.
Now that I have placed data, model, view, store, controller inside my app folder and have used the 'url: 'app/data/users.json', still my grid doesn't load the datas :( and the console.log(this.url) is still undefined. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.