[英]google ajax search with jQuery - $.getJSON(). there is any response
i'm trying to make my image search with google ajax search i'm using jQuery. 我正在尝试用google ajax搜索我的图像搜索我正在使用jQuery。 so below my code 所以低于我的代码
$.getJSON('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=hello', function(data) {
console.log(data);
});
the console printed NULL and my xhr information is 控制台打印NULL并且我的xhr信息是
request URL:http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=hello
Request Headers
Accept:application/json, text/javascript, */*
Cache-Control:max-age=0
Origin:http://example.local
Referer:http://example.local/thread/create
User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Safari/533.4
i'm not sure what's wong. 我不确定是什么。 help me plz 帮帮我
Your URL needs a slight tweak to trigger JSONP, add &callback=?
您的URL需要稍微调整才能触发JSONP,添加&callback=?
on the end, like this: 最后,像这样:
$.getJSON('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=hello&callback=?', function(data) {
console.log(data);
});
You can see it working here , take a look at the console. 你可以看到它在这里工作 ,看看控制台。
If jQuery sees a callback=?
如果jQuery看到callback=?
in the url, it replaces it with a function name it generates (which is your function(data)
callback), and that's what gets run when the JSONP request comes back. 在url中,它用它生成的函数名(它是你的function(data)
回调function(data)
替换它,这就是当JSONP请求返回时运行的function(data)
。 See the $.getJSON()
documentation for the same info. 有关相同信息,请参阅$.getJSON()
文档 。
Without this it's trying to do an XmlHttpRequest, and being blocked by the same-origin policy , since it's on another domain. 如果没有这个,它会尝试执行XmlHttpRequest,并被同源策略阻止,因为它在另一个域上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.