通过jQuery获取JSONP [英] Getting JSONP via jQuery

查看:61
本文介绍了通过jQuery获取JSONP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新1:

这是我在浏览器中输入的内容

This is what I get in the browser if I type

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吗?

So this is not jsonp?

原始问题:

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

I have the following script where I am trying to get json data from a remote host:

$(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");
            }
        });
    }
});

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

But for some reason I am getting an error and warning:

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

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

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

Error: Uncaught SyntaxError: Unexpected token :

我在做什么错了?

推荐答案

JSONP协议"依赖于网站以形式为JavaScript的语句答复您的请求,

The JSONP "protocol" relies on the site replying to your request with a JavaScript statement of the form,

 someFunction( someJSON )

函数名称作为代码中的参数提供,其思想是响应脚本一旦被浏览器使用和解释,将导致使用JSON解析的blob对该函数的调用.也就是说,一个JavaScript对象. jQuery库将为您完成一些boolean工作,甚至达到创建要调用的全局范围函数的程度(这将是仅将您提供的回调作为成功"参数调用的代码).

The name of the function is supplied as an argument from your code, with the idea being that the response script, once consumed and interpreted by the browser, will result in a call to that function with a parsed blob of JSON — which is to say, a JavaScript object. The jQuery library will do some of the bookeeping work for you, even to the extent of creating the globally-scoped function to call (which will be code that just calls the callback you supply as the "success" argument).

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

Thus, you should check what the actual response from that server looks like. It sounds to me as if it may not be a server prepared to respond that way. You might need to make sure there's an extra parameter on your URL, of the form "callback=?".

这篇关于通过jQuery获取JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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