【问题标题】:Get variable from Sencha Touch Ext.data.JSONP.request response从 Sencha Touch Ext.data.JSONP.request 响应中获取变量
【发布时间】:2013-07-20 17:31:55
【问题描述】:

我试图从对Ext.data.JsonP.request 的响应中获取布尔值(成功)和 HTML 字符串(结果),但我不知道如何。这是我到目前为止的代码:

Ext.data.JsonP.request({
    url: 'http://wereani.ml/shorten-app.php',
    callbackKey: 'callback',
    params: {
        data: Ext.encode(values)
    },
    success: function(response) {
        console.log(response);
        console.log(JSON.stringify(values));
        console.log('Link Shortened');
        if(response.responseText['success'] == true) {
        Ext.Msg.alert('Link Shortened', response.responseText, Ext.emptyFn);
        } else {
            Ext.Msg.alert('Error', response.responseText, Ext.emptyFn);
        }
        form.reset();
    },
    failure: function(response) {
        console.log(response);
        console.log(JSON.stringify(values));
        console.log('Error');
        Ext.Msg.alert('Error', 'Please try again.', Ext.emptyFn);
    }
});

一些示例 JSON-P:

callback({"success":false,"result":"<div class=\"error\">Please enter a valid link to shorten!<\/div>"})

我目前遇到的错误是Uncaught TypeError: Cannot read property 'success' of undefined。 提前感谢您的帮助!

编辑:似乎responseObject {success: false, result: "&lt;div class="error"&gt;Please enter a valid link to shorten!&lt;/div&gt;"}response.responseTextundefined。我使用了错误的变量吗?我在 response 上找不到任何文档。

编辑 2:Console.log(response.responseText) 返回 undefined 的原因似乎是因为它.... 是。我传递的 JsonP 编码变量(successresult)被自动解析到对象中,并且从未创建过 response.responseText(尽管我认为它应该是)。解决方案是直接阅读response.successresponse.result

回答:这是有效的代码。感谢 Viswa 的帮助!

Ext.data.JsonP.request({
    url: 'http://wereani.ml/shorten-app.php',
    callbackKey: 'callback',
    params: {
        data: Ext.encode(values)
    },
    success: function(response) {
        if(response.success === true) {
            Ext.Msg.alert('Link Shortened', response.result, Ext.emptyFn);
        } else {
            Ext.Msg.alert('Error', response.result, Ext.emptyFn);
        }
        form.reset();
    },
    failure: function(response) {
        console.log('Error');
        Ext.Msg.alert('Error', '<div class="error">Please try again.</div>', Ext.emptyFn);
    }
});

【问题讨论】:

    标签: html sencha-touch sencha-touch-2 jsonp sencha-architect


    【解决方案1】:

    这样试试

    Ext.data.JsonP.request({
        url: 'http://example.com/script.php',
        callbackKey: 'callback',
        params: {
            data: Ext.encode(values)
        },
        success : function(response) {
          console.log("Spiffing, everything worked");
          // success property
          console.log(response.success);
          // result property
          console.log(response.result);
       },
       failure: function(response) {
            console.log(response);
            Ext.Msg.alert('Error', 'Please try again.', Ext.emptyFn);
        }
    });
    

    【讨论】:

    • 看来response.responseText 一直在返回undefined。我已经编辑了问题以添加更多详细信息。我使用了错误的变量吗?
    • 我想通了! response.responseText 不存在,但我的 JsonP 变量存在。似乎额外的JSON.parse 仅对其他类型或请求是必需的。感谢您的帮助!
    • 很高兴你想通了,你能用你想出的更新我的答案吗.. 这样它可以帮助别人或用答案更新你的问题
    • 我将编辑问题,这样我就可以复制和粘贴我的新代码,并且更容易看到差异。
    • 我也可以更新你的答案,以防以后对其他人有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    相关资源
    最近更新 更多