通过jQuery获取JSONP



更新1:

如果我输入,这就是我在浏览器中得到的

http://www.remote_host.com/feed.php?callback=jsonpCallBack

{
    "rss": {
        "channels": [
            {
                "title": "title goes here",
                "link": "http://www.remote_server.com/feed.php",
                "description": "description goes here",
                "items": [
                    {
                        "title": "item title goes here",
                        "link": "item link goes here",
                        "pubDate": "item date goes here",
                        "description": "item description goes here"
                    },
                    {
                        "title": "item title goes here",
                        "link": "item link goes here",
                        "pubDate": "item date goes here",
                        "description": "item description goes here"
                    },
                    {
                        "title": "item title goes here",
                        "link": "item link goes here",
                        "pubDate": "item date goes here",
                        "description": "item description goes here"
                    }
                ]
            }
        ]
    }
}

所以这不是jsonp?

原始问题:

我有以下脚本,试图从远程主机获取json数据:

$(document).ready(function() {
    get_json_feed();
    function get_json_feed() {
        $.ajax({
            url: 'http://www.remote_host.com/feed.php?type=json',
            type: 'GET',
            dataType: 'jsonp',
            error: function(xhr, status, error) {
                alert("error");
            },
            success: function(json) {
                alert("success");
            }
        });
    }
});

但出于某种原因,我收到了一个错误和警告:

警告:资源解释为脚本,但使用MIME类型传输text/html。

错误:未捕获语法错误:意外的令牌:

我做错了什么?

JSONP"协议"依赖于网站使用形式的JavaScript语句来回复您的请求

 someFunction( someJSON )

函数的名称是作为代码中的一个参数提供的,其思想是,一旦浏览器使用并解释了响应脚本,就会使用解析后的JSON blob调用该函数—也就是说JavaScript对象。jQuery库将为您做一些记账工作,甚至创建要调用的全局范围函数(这将是只调用您作为"成功"参数提供的回调的代码)。

因此,您应该检查来自该服务器的实际响应是什么样子的。在我看来,这可能不是一个准备以这种方式响应的服务器。您可能需要确保URL上有一个额外的参数,形式为"callback=?"。

我不知道您到底面临什么错误,但这里有一些使用jsonp的有用提示

  • error:不为跨域脚本和JSONP请求调用此处理程序
  • 在ajax参数中写入jsonp: 'callback', jsonpCallback: 'jsonpCallback'。将jsonp设置为callback,然后将jsonpCallback设置为jsonpCallback,查询字符串如下所示:

    http://domain.com/jsonp-demo.php?callback=jsonpCallback&name=每次

  • 使用JSONP在JSON块中加载。将在URL的末尾添加一个额外的?callback=?来指定回调。

您的完整脚本如下所示:

<script>
    $(document).ready(function(){
        $("#useJSONP").click(function(){
            $.ajax({
                url: 'http://domain.com/jsonp-demo.php',
                data: {name: 'Chad'},
                dataType: 'jsonp',
                jsonp: 'callback',
                jsonpCallback: 'jsonpCallback',
                success: function(){
                    alert("success");
                }
            });
        });
    });
    function jsonpCallback(data){
        $('#jsonpResult').text(data.message);
    }
    </script>

此处的示例

看起来服务器返回了错误的内容类型标头。

最新更新