【问题标题】:Sencha Touch cross-domain jsonp returns a syntax errorSencha Touch跨域jsonp返回语法错误
【发布时间】:2014-01-08 21:16:09
【问题描述】:

我有以下代码从另一个域检索 JSON 结构并将其显示在页面上。

但是,什么都没有出现,我在控制台中收到此错误:

Uncaught SyntaxError: Unexpected token : People.json:2

我已将我的 JSON 传递给 JSON 验证器,它返回为有效。

这是我的代码:

    Ext.define("Person", {
        extend: "Ext.data.Model",
        config: {
            fields: [
                { name: 'Id', type: 'int'},
                { name: 'Name', type: 'string'},
                { name: 'Email', type: 'string'}
            ]
        }
    });

    var myStore = Ext.create("Ext.data.Store", {
        model: "Person",
        proxy: {
            type: "jsonp",
            url: 'http://example.com/People.json',
            reader: {
                type: "json",
                rootProperty: "Person"
            }
        },
        autoLoad: true
    });

    Ext.create("Ext.List", {
        fullscreen: true,
        store: myStore,
        itemTpl: "{Name}, {Id}"
    });

这是我的 JSON 文件的内容:

{
    "Person": [
        {
            "Id": 0,
            "Name": "Robert Lara",
            "Email": "rolara@example.com"
        },
        {
            "Id": 1,
            "Name": "Tom Hicks",
            "Email": "tohicks@example.com"
        }
    ]
}

【问题讨论】:

    标签: javascript json extjs sencha-touch cross-domain


    【解决方案1】:

    JSONP 并不是真正地传输 JSON,它希望 http://example.com/People.json 是调用回调函数的 Javascript 文件。

    您已经使用 Sencha 处理了 JSONP 的客户端部分,但您必须准备服务器端才能使用它。

    Sencha docs on JSONP在标题下有一段和一些代码示例在服务器端实现

    另外this StackOverflow question 解释了 JSONP 的全部内容。

    【讨论】:

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