1
votes

with extjs,i m runnig the server from one system,giving the url in another system and i m using JSONP in extjs ver:4.02,when i check in response i m getting data in json format,when i try to print in console or Store im not getting..here is my extjs code...

<script type="text/javascript">

            Ext.Ajax.cors = true;
            Ext.Ajax.useDefaultXhrHeader = false;   

        Ext.define('User', {
            extend: 'Ext.data.Model',
            fields: ['empid', 'name', 'email']
        });

    var myStore = Ext.create('Ext.data.Store', {
                    model: 'User',                     
                    autoLoad:true,
                    proxy: {
                        type: 'jsonp',

                      // url : 'data/tagfamily.json',
                        url:'http://192.168.7.70:8080/palamanagement/user/getAllRecords',

                                            },
                    listeners:{
                        'load':function( store, records, successful, eOpts ){                                             
                            alert(records);
                            console.log(records);

                        }
                    }
                });

2
Due to the nature, you should provide callback function on second domain. Read carefully doc pls. docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.proxy.JsonP - Oğuz Çelikdemir
What does the response from your server look like? It needs to be formatted as a callback that the proxy can execute. See the docs (and example response) for the JsonP proxy here: docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.proxy.JsonP - existdissolve
@existdissolve thanks for your reply :[{"empid":1,"age":12,"department":"sme","email":"[email protected]","name":"murugan","phone":4565465441,"designation":"software engineer"},{"empid":2,"age":23,"department":"sme","email":"[email protected]","name":"Jessyfhgfh","phone":4423023222,"designation":"Software Engineer"}] this is my output data in response, how to use callback function ? - Sathiyaraj
@OğuzÇelikdemir thanks for the reply,can u give any specific reason or example, why should i use callBack().... - Sathiyaraj

2 Answers

2
votes

You literally have to return something like the following:

someCallback({
    users: [
        {
            id: 1,
            name: "Ed Spencer",
            email: "[email protected]"
        }
    ]
});

So you already have the JSON the way you need it; you just need your server response to wrap the JSON in the callback so that the JSONP proxy can execute and load your store with data.

Therefore, when handling the JSONP request, your server's script needs to recognize the "callback" param that is sent as a part of the request. It then needs to use the value of this param to wrap your JSON response.

Be sure to read the docs that both myself and Oguz have posted: they outline the requirement pretty well. But if you don't respond with a callback, you'll never get your standard JSON response to work with the JSONP proxy.

0
votes

When you request from DOMAIN-A to DOMAIN-B, you should provide a call back function in proxy definition. The callback function will use in after request complete.

For instance, in Flickr REST service, there is jsoncallback parameter which we should give our function name in order to complete our request. In this way, our request url will be:

.../?jsoncallback=ourFunction

To be able to provide our function name, there is a property in ExtJS which is callbackKey. Like so:

Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: ['empid', 'name', 'email']
});

Ext.data.JsonP.request('http://api.flickr.com/services/feeds/photos_public.gne', {
    callbackKey: 'jsoncallback',
    callback: ourFunctionName
});

or

Ext.data.JsonP.request('http://api.flickr.com/services/feeds/photos_public.gne', {
    callbackKey: 'jsoncallback',
    callback: function(data) {
        ...
    }
});    

PS: I know, you will not find callback property in doc but just to be sure there is. Check ext-all-debug-w-comments.js file, line 108486

Flickr Callback Function
JsonP callbackKey