Sencha Touch:如何使用JSONP获得简单的json文件作为响应? [英] Sencha Touch : How to get the simple json file as response using JSONP?

查看:93
本文介绍了Sencha Touch:如何使用JSONP获得简单的json文件作为响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行一个简单的JSONP调用,以获取一个在远程服务器上加载的json文件.

I am trying to make a simple JSONP call to get a json file which is loaded on the remote server.

这是我在服务器上加载的简单json文件.

Here is my simple json file loaded on the server.

{
    "login": [
        {
            "themename": "NO",
            "themeId": "1"
        }
    ],
    "homePage": [
        {
            "themename": "NO",
            "themeId": "1"
        }
    ],
    "transactionDetails": [
        {
            "themename": "NO",
            "themeId": "1"
        }
    ]
}

我的控制器代码,该代码调用此文件以获取数据

My Controller code which calls this file to get the data

 Ext.data.JsonP.request(
    {

    url : 'http://xx.xx:8080/ThemeSelector.json',

            callback : 'someCallback' ,

            someCallback: function(success, result) {

            var text = result.responseText;

              var object = Ext.decode(text);
              themeName = object['homePage'][0].themename;

            }
        });

我收到错误"Uncaught SyntaxError: Unexpected token : "

我知道响应应该包装在对象中,但不能在json文件和我的代码中进行准确的更正.有什么帮助吗?

I know that the response should be wrapped in the object but not able to make the exact correction in json file and my code. Any help please?

谢谢

推荐答案

要使用JsonP,您的json响应应包含您发送的callback参数.否则,callback函数将不会被调用并产生错误,因为它确实需要这样做.在您的情况下,您只需在服务器上提供纯JSON文件即可.因此,您不能直接与此文件一起使用JsonP.

To work with JsonP, your json response should contain the callback parameter you've sent. Without that, callback function will not get called and produces error since it does require that. In your case, you just have plain JSON file on server to serve. So you cant use JsonP directly with this file.

如果您可以控制服务器,则可以编写一个脚本来满足您的需要-

If you've some control over server, then you can write a script that can do this for you like -

<?php
 header('Content-Type: text/javascript');
 $response = file_get_contents('ThemeSelector.json');
 echo $_GET['someCallback'] . '(' . $response .' );';
?>

然后收到的json响应将类似于-

Then received json response will look something like -

Ext.data.JsonP.callback2 (
    {
        "login": [
            {
                "themename": "NO",
                "themeId": "1"
            }
        ],
        "homePage": [
            {
                "themename": "NO",
                "themeId": "1"
            }
        ],
        "transactionDetails": [
            {
                "themename": "NO",
                "themeId": "1"
            }
        ]
    }
)

这篇关于Sencha Touch:如何使用JSONP获得简单的json文件作为响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆