我是sencha touch 2的新手。我无法设置我的qrCodeHtml变量并在JsonP请求之外使用它。我知道所有这些代码都可以工作,除了能够设置qrCodeHtml变量。请帮助我完成此任务:

    onMyProfileCommand: function () {
    var api_username = "o_xxxxx";
    var api_key = "R_xxxxxxxxxx";
    var long_url = "http://google.com";
    var qrCodeHtml = '';

    Ext.data.JsonP.request({
        url: 'https://api-ssl.bitly.com/v3/shorten',
        callbackKey: 'callback',
        params: {
            login: api_username,
            apiKey: api_key,
            longUrl: long_url
        },
        success: function (result, request) {
            var shortUrl = result.data.url;
            qrCodeHtml = '<div style="font-size:15px; margin-bottom:5px;">Friends can scan this to <a href="' + shortUrl +
                             '" style="color:inherit; text-decoration:none; font-weight:bold;">view your profile!</a></div><img src="' +
                             shortUrl + '.qrcode" style="height:110px; width:110px;" />';
        }
    });

    this.getLblQrCodeHtml().setData({ QrCodeHtml: qrCodeHtml });
    Ext.Viewport.animateActiveItem(this.getProfileView(), this.slideLeftTransition);
}

最佳答案

谢谢Goyuix,正是我所需要的!解决方法如下:

    onMyProfileCommand: function () {
var api_username = "o_xxxxx";
var api_key = "R_xxxxxxxxxx";
var long_url = "http://google.com";
var controller = this;

Ext.data.JsonP.request({
    url: 'https://api-ssl.bitly.com/v3/shorten',
    callbackKey: 'callback',
    params: {
        login: api_username,
        apiKey: api_key,
        longUrl: long_url
    },
    success: function (result, request) {
        var shortUrl = result.data.url;
        var qrCodeHtml = '<div style="font-size:15px; margin-bottom:5px;">Friends can scan this to <a href="' + shortUrl +
                         '" style="color:inherit; text-decoration:none; font-weight:bold;">view your profile!</a></div><img src="' +
                         shortUrl + '.qrcode" style="height:110px; width:110px;" />';

        controller.getLblQrCodeHtml().setData({ QrCodeHtml: qrCodeHtml });
    }
});

Ext.Viewport.animateActiveItem(controller.getProfileView(), this.slideLeftTransition);


}

关于javascript - Sencha Touch:如何在JsonP请求中设置变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13778984/

10-11 23:25
查看更多