通过 jQuery 获取 JSONP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6213853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting JSONP via jQuery
提问by oshirowanen
UPDATE 1:
更新1:
This is what I get in the browser if I type
这是我在浏览器中输入的内容
http://www.remote_host.com/feed.php?callback=jsonpCallBack
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"
}
]
}
]
}
}
So this is not jsonp?
所以这不是jsonp?
ORIGINAL QUESTION:
原问题:
I have the following script where I am trying to get json data from a remote host:
我有以下脚本,我试图从远程主机获取 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");
}
});
}
});
But for some reason I am getting an error and warning:
但由于某种原因,我收到错误和警告:
Warning: Resource interpreted as Script but transferred with MIME type text/html.
Error: Uncaught SyntaxError: Unexpected token :
警告:资源被解释为脚本但使用 MIME 类型 text/html 传输。
错误:未捕获 SyntaxError:意外标记:
What am I doing wrong?
我究竟做错了什么?
回答by Pointy
The JSONP "protocol" relies on the site replying to your request with a JavaScript statement of the form,
JSONP“协议”依赖于使用表单的 JavaScript 语句回复您的请求的站点,
someFunction( someJSON )
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).
函数的名称作为代码中的参数提供,其想法是响应脚本一旦被浏览器使用和解释,将导致使用解析的 JSON blob 调用该函数 - 也就是说,一个 JavaScript 对象。jQuery 库将为您完成一些簿记工作,甚至创建要调用的全局范围函数(这些代码仅调用您作为“成功”参数提供的回调)。
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 mightneed to make sure there's an extra parameter on your URL, of the form "callback=?".
因此,您应该检查来自该服务器的实际响应是什么样的。在我看来,它可能不是准备以这种方式响应的服务器。您可能需要确保您的 URL 上有一个额外的参数,形式为“callback=?”。
回答by diEcho
I don't know exactly what error you are facing, but there are some useful tips for using jsonp
here
我不知道你所面临的到底是什么错误,但也有使用一些有用的技巧jsonp
在这里
error
: This handler is not called for cross-domain script and JSONP requests.write
jsonp: 'callback', jsonpCallback: 'jsonpCallback'
in ajax parameters. Settingjsonp
to callback and then settingjsonpCallback
to jsonpCallback makes the querystring look like this:http://domain.com/jsonp-demo.php?callback=jsonpCallback&name=watever
Loads in a JSON block using JSONP. Will add an extra
?callback=?
to the end of your URL to specify the callback.
error
:跨域脚本和 JSONP 请求不会调用此处理程序。写入
jsonp: 'callback', jsonpCallback: 'jsonpCallback'
ajax参数。设置jsonp
为回调然后设置jsonpCallback
为 jsonpCallback 使查询字符串如下所示:http://domain.com/jsonp-demo.php?callback=jsonpCallback&name=watever
使用 JSONP 加载到 JSON 块中。将
?callback=?
在您的 URL 末尾添加额外内容以指定回调。
Your complete script would look like this:
您的完整脚本如下所示:
<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>
回答by bjornd
Looks like server returns wrong Content-type header.
看起来服务器返回错误的 Content-type 标头。