解析json时出现意外错误

时间:2014-02-27 19:18:53

标签: javascript json extjs sencha-touch openmrs

使用给定here的教程,我正在创建一个应用程序来从URL获取json数据并显示它。我正在使用此代码调用URL并进行解析。

{
                    xtype: 'nestedlist',
                    title: 'Blog',
                    iconCls: 'star',
                    cls: 'blog',
                    displayField: 'title',

                    store: {
                        type: 'tree',

                        fields: ['uuid', 'display'


                        ],

                        root: {
                            leaf: false
                        },

                        proxy: {
                            type: 'scripttag',
                            url: 'http://localhost:8081/openmrs-standalone/ws/rest/v1/location',
                            reader: {
                                type: 'json',
                                rootProperty: 'results'
                            }
                        },

                    },

                },

在控制台中,我收到了

形式的回复
{
    "results": [
        {
            "uuid": "c0937f0c-1691-11df-97a5-7038c432aabf",
            "display": "Chulaimbo",
            "links": [
                {
                    "uri": "http://localhost:8081/openmrs-standalone/ws/rest/v1/location/c0937f0c-1691-11df-97a5-7038c432aabf",
                    "rel": "self"
                }
            ]
        },
        {
            "uuid": "c0937d4f-1691-11df-97a5-7038c432aabf",
            "display": "Mosoriot Hospital",
            "links": [
                {
                    "uri": "http://localhost:8081/openmrs-standalone/ws/rest/v1/location/c0937d4f-1691-11df-97a5-7038c432aabf",
                    "rel": "self"
                }
            ]
        },
        {
            "uuid": "8d6c993e-c2cc-11de-8d13-0010c6dffd0f",
            "display": "Unknown Location",
            "links": [
                {
                    "uri": "http://localhost:8081/openmrs-standalone/ws/rest/v1/location/8d6c993e-c2cc-11de-8d13-0010c6dffd0f",
                    "rel": "self"
                }
            ]
        }


    ]
}

但显示错误enter image description here “location”是服务的名称。

2 个答案:

答案 0 :(得分:0)

控制台认为您正在尝试将JSON作为代码执行。所以在第一个{之后,它需要指令。不是数据。试着把括号括在整个事物上:

{ myfield: 1, anotherfield: 2 } (ERROR)

({ myfield: 1, anotherfield: 2 }) (SUCCESS)

这将解决问题。

答案 1 :(得分:0)

我使用不同的方法完成了它。我使用proxytype'rest'并在同一服务器和端口上运行OpenMRS和我的应用程序,因为我的OpenMRS实例不允许跨域调用。这个问题的问题是使用type作为“scripttag”,我仍然不知道它有什么问题。使用“rest”解决它。

相关问题