通过jQuery获取JSONP

前端之家收集整理的这篇文章主要介绍了通过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","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");
            }
        });
    }
});

但由于某种原因,我收到错误并警告:

Warning: Resource interpreted as
Script but transferred with MIME type
text/html.

Error: Uncaught SyntaxError:
Unexpected token :

我究竟做错了什么?

解决方法

JSONP“协议”依赖于站点使用表单的JavaScript语句回复您的请求,
someFunction( someJSON )

函数名称是作为代码提供的一个参数提供的,其理念是响应脚本一旦被浏览器使用和解释,将导致使用已解析的JSON blob调用函数 – 也就是说,一个JavaScript对象. jQuery库将为您完成一些bookeeping工作,甚至可以创建要调用的全局范围函数(这将是仅调用您提供的回调作为“success”参数的代码).

因此,您应该检查该服务器的实际响应是什么样的.听起来好像它可能不是准备以这种方式响应的服务器.您可能需要确保URL上有一个额外的参数,形式为“callback =?”.

原文链接:https://www.f2er.com/jquery/176956.html

猜你在找的jQuery相关文章