My code is below:
我的代码如下:
$.getJSON('https://ajax.googleapis.com/ajax/services/search/images?q=Google&v=1.0',
function(json) {
alert(json);
})
You can try this code here: http://jsbin.com/ofaru3/edit
您可以在此处尝试此代码:http://jsbin.com/ofaru3/edit
The ajax is error
ajax是错误的
imagesFailed to load resource
imagesFailed加载资源
How cna I fix this problem? Thanks!
怎么解决这个问题呢?谢谢!
1 个解决方案
#1
6
You need &callback=?
on the URL there to trigger JSONP, like this:
你需要和回调=?在那里触发JSONP的URL,如下所示:
$.getJSON('https://ajax.googleapis.com/ajax/services/search/images?q=Google&v=1.0&callback=?',
function(json) {
alert(json);
});
You can test it out here. Without the &callback?
it's trying to fetch the data from a remote domain with an XmlHttpRequest (AJAX) and failing/being blocked due to the same origin policy. This is exactly the type of situation JSONP is for.
你可以在这里测试一下。没有&回调?它试图使用XmlHttpRequest(AJAX)从远程域获取数据,并且由于相同的原始策略而导致失败/被阻止。这正是JSONP的用途。
From the $.getJSON()
docs:
来自$ .getJSON()文档:
JSONP
If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of thejsonp
data type in$.ajax()
for more details.JSONP如果URL包含字符串“callback =?” (或类似的,由服务器端API定义),请求被视为JSONP。有关更多详细信息,请参阅$ .ajax()中有关jsonp数据类型的讨论。
#1
6
You need &callback=?
on the URL there to trigger JSONP, like this:
你需要和回调=?在那里触发JSONP的URL,如下所示:
$.getJSON('https://ajax.googleapis.com/ajax/services/search/images?q=Google&v=1.0&callback=?',
function(json) {
alert(json);
});
You can test it out here. Without the &callback?
it's trying to fetch the data from a remote domain with an XmlHttpRequest (AJAX) and failing/being blocked due to the same origin policy. This is exactly the type of situation JSONP is for.
你可以在这里测试一下。没有&回调?它试图使用XmlHttpRequest(AJAX)从远程域获取数据,并且由于相同的原始策略而导致失败/被阻止。这正是JSONP的用途。
From the $.getJSON()
docs:
来自$ .getJSON()文档:
JSONP
If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of thejsonp
data type in$.ajax()
for more details.JSONP如果URL包含字符串“callback =?” (或类似的,由服务器端API定义),请求被视为JSONP。有关更多详细信息,请参阅$ .ajax()中有关jsonp数据类型的讨论。