extjs using jsonp,Accessing data from remote url

99封情书 提交于 2021-02-08 09:19:11

问题


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);

                        }
                    }
                });


回答1:


You literally have to return something like the following:

someCallback({
    users: [
        {
            id: 1,
            name: "Ed Spencer",
            email: "ed@sencha.com"
        }
    ]
});

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.




回答2:


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



来源:https://stackoverflow.com/questions/21702792/extjs-using-jsonp-accessing-data-from-remote-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!