【问题标题】:How to call JSONP request in Sencha Touch application如何在 Sencha Touch 应用中调用 JSONP 请求
【发布时间】:2012-08-04 06:48:22
【问题描述】:

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

谢谢你

【问题讨论】:

标签: sencha-touch


【解决方案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...
        }
    });

【讨论】:

    【解决方案2】:

    检查下面的代码对我来说可以正常工作:)

                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();
                                        }
                                    });
                                }
                            }]
                        }]
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      相关资源
      最近更新 更多