Как вызвать JSONP-запрос в приложении Sencha Touch - PullRequest
1 голос
/ 06 марта 2012

В моем приложении sencha мне нужно вызывать jsonp-запрос вместо ajax-запроса, но я не знаю, как его написать. Поэтому, пожалуйста, предоставьте мне демо-версию для jsonp-запроса.

спасибо

Ответы [ 2 ]

1 голос
/ 04 августа 2012

проверьте, что код ниже работает нормально для меня:)

            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();
                                    }
                                });
                            }
                        }]
                    }]
                }
            });
1 голос
/ 12 апреля 2012

Пример запроса JSON из документации Sencha :) Вот ссылка на более подробную информацию: 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...
        }
    });
...