如何在Sencha Touch应用程序中调用JSONP请求

时间:2012-03-06 07:57:58

标签: sencha-touch

在我的sencha应用程序中,我需要调用jsonp请求而不是ajax请求,但我不知道如何编写它。所以请为我提供jsonp请求的演示。

谢谢你

2 个答案:

答案 0 :(得分:1)

来自Sencha文档的示例JSON请求:) 以下是更多详细信息的链接:http://docs.sencha.com/touch/2-0/#!/api/Ext.data.JsonP

Ext.data.JsonP.request({
        url: 'http://free.worldweatheronline.com/feed/weather.ashx',
        callbackKey: 'callback',
        params: {
            key: '23f6a0ab24185952101705',
            q: '94301', // Palo Alto
            format: 'json',
            num_of_days: 5
        },
        success: function(result) {
            //Your success function here...
        }
    });

答案 1 :(得分:1)

检查以下代码对我来说没问题:)

            Ext.define('APP.view.List', {
                extend: 'Ext.Container',
                requires: [
                    'Ext.data.JsonP'
                ],
                config: {
                    scrollable: true,
                    items: [{
                            xtype: 'panel',
                            id: 'JSONP'
                        }, {
                        docked: 'top',
                        xtype: 'toolbar',
                        items: [{
                            text: 'Load using JSON-P',
                            handler: function() {
                                var panel = Ext.getCmp('JSONP'),
                                    tpl = new Ext.XTemplate([
                                    '<div class="demo-weather">',
                                        '<tpl for=".">',
                                            '<div class="day">',
                                                '<div class="date">{date}</div>',
                                                '<tpl for="weatherIconUrl">',
                                                    '<img src="{value}">',
                                                '</tpl>',
                                                '<span class="temp">{tempMaxF}°<span class="temp_low">{tempMinF}°</span></span>',
                                            '</div>',
                                        '</tpl>',
                                    '</div>'
                                ]);

                                panel.getParent().setMasked({
                                    xtype: 'loadmask',
                                    message: 'Loading...'
                                });

                                Ext.data.JsonP.request({
                                    url: 'http://free.worldweatheronline.com/feed/weather.ashx',
                                    callbackKey: 'callback',
                                    params: {
                                        key: '23f6a0ab24185952101705',
                                        q: '94301', // Palo Alto
                                        format: 'json',
                                        num_of_days: 5
                                    },

                                    callback: function(success, result) {
                                        var weather = result.data.weather;

                                        if (weather) {
                                            panel.updateHtml(tpl.applyTemplate(weather));
                                        }
                                        else {
                                            alert('There was an error retrieving the weather.');
                                        }

                                        panel.getParent().unmask();
                                    }
                                });
                            }
                        }]
                    }]
                }
            });