如何从 jQuery 中的外部 URL 获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3629254/
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
How to get data from an external URL in jQuery?
提问by Kurund Jalmi
var resturl = "http://example.com";
cj.getJSON(
resturl + "&callback=?",
function(data)
{
console.log( data );
}
);
My callback function is never called. Any ideas?
我的回调函数从未被调用。有任何想法吗?
回答by Nick Craver
Two things here:
这里有两件事:
First your URL additon should be "?callback=?"
since there's no other querystring, or use the full $.ajax()
call with a jsonp
data type so it adds the querystring as needed, like this:
首先,您的 URL additon 应该是"?callback=?"
因为没有其他查询字符串,或者使用$.ajax()
具有jsonp
数据类型的完整调用,以便根据需要添加查询字符串,如下所示:
$.ajax({
url: resturl,
dataType: 'jsonp',
success: function(data){
console.log( data );
}
});
Second, the domain you're going to has to support JSONP.
其次,您要使用的域必须支持JSONP。
回答by Coach John
Remember that jquery is going to execute your callback as a function, so you've got to make sure that the JSON returned by your server is wrapped as a callback.
请记住,jquery 会将您的回调作为函数执行,因此您必须确保服务器返回的 JSON 被包装为回调。
Here's a very simple working example.
这是一个非常简单的工作示例。
Javascript:
Javascript:
var myURL = "http://www.someserver.com/somegreatapplication.php?callback=?";
$.getJSON( myURL, function(data) {
$("#somediv").html( data.htmlCode);
});
somegreatapplication.php
一些很棒的应用程序.php
<?php
$output['htmlCode'] = "<b>This worked!</b>";
// Properly format the jsonP response //
$json = json_encode( $output);
header("Content-type: application/json");
exit( $_GET['callback'] . ' (' . $json . ');' );
?>
Notice that the PHP will not return Raw JSON code like you're probably used to, but will actually return the callback function, including the JSON. jQuery will turn your URL into something like this:
请注意,PHP 不会像您习惯的那样返回原始 JSON 代码,但实际上会返回回调函数,包括 JSON。jQuery 会将你的 URL 变成这样的:
http://www.someserver.com/somegreatapplication.php?callback=jsonp1283446768493&_=1283446768526
http://www.someserver.com/somegreatapplication.php?callback=jsonp1283446768493&_=1283446768526
Your PHP would then output this:
然后你的 PHP 会输出这个:
jsonp1283446768493({"menuHTML":"<b>This worked!</b>"});
That gets run, and then the object is available within your getJSON( ... function(){} );
运行,然后对象在您的 getJSON( ... function(){} ) 中可用;
All of this of course assumes that you're controlling both ends of the communication. If you don't, then you need to make sure that the other end is giving you proper JSONP output (you may need to use a specifically named callback if they are forcing that)
当然,所有这些都假设您正在控制通信的两端。如果您不这样做,那么您需要确保另一端为您提供正确的 JSONP 输出(如果他们强制使用,您可能需要使用特定命名的回调)
回答by Adam
This should work as long as the server side sends the data and uses JSONP
只要服务器端发送数据并使用 JSONP,这应该可以工作
var resturl = "http://example.com";
$.getJSON(resturl,{"callback":"?"},function(data, textStatus){ console.log(data)});
回答by Horst Gutmann
According to the documentation the callback is only executed on success. So the request in general might have failed. The URL that is constructed also doesn't really contain a valid query string. You start the query string with a ?
, but your URL just contains a &
which indicates additional query string parameters:
根据文档,回调仅在成功时执行。所以一般来说请求可能失败了。构造的 URL 也并不真正包含有效的查询字符串。您以 a 开头查询字符串?
,但您的 URL 仅包含&
指示附加查询字符串参数的 a:
callback(data, textStatus)A callback function that is executed if the request succeeds.
callback(data, textStatus) 请求成功时执行的回调函数。
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.getJSON/
But I'm not sure if the additional JSONPoption changes something about this policy.
但我不确定额外的JSONP选项是否会改变有关此政策的某些内容。