I know this has been asked a zillion times, but I still can't get my code to work. I am trying to a simple JSONP call from my Javascript application. The cod fragment looks like:
我知道这已被问过无数次,但我仍然无法让我的代码工作。我正在尝试从我的Javascript应用程序中进行简单的JSONP调用。鳕鱼片段看起来像:
url="http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=?";
and then either:
然后是:
$.getJSON(url, function(data) {
alert('hello 1');
});
or:
要么:
$.ajax({url: url,
datatype: 'jsonp',
success: function(data) { alert("hello 2"); },
error: function(j, t, e) { alert(t);}
});
Neither approach works. The second approach results in the alert of "error". The first does not return success either. What am I doing wrong? Many, many thanks!!
这两种方法都不奏效。第二种方法导致“错误”的警报。第一个也没有成功。我究竟做错了什么?非常感谢!!
UPDATE: I think I found at least one problem. Let me look more into this.
更新:我想我至少发现了一个问题。让我看看这个。
UPDATE 2: Sorry, this code actually works, at least the first approach. There was a subtle error around this code fragment that resulted in the code not working, but overall this works just fine. Asynchronous calls are sometimes a little tricky :-)
更新2:对不起,这段代码实际上是有效的,至少是第一种方法。这个代码片段周围有一个微妙的错误导致代码不能正常工作,但整体而言这很好。异步调用有时候有点棘手:-)
2 个解决方案
#1
5
Check this out JsFIddleDemo
看看JsFIddleDemo
/*
* create callbak function for jsonP
* @params
* data is response from http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=myjsonpfunction
*/
function myjsonpfunction(data){
alert(data.responseData.results) //showing results data
$.each(data.responseData.results,function(i,rows){
alert(rows.url); //showing results url
});
}
//request data using jsonP
$(function(){
$.ajax({
url:'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=myjsonpfunction',
type:"GET",
dataType: 'jsonp',
jsonp: 'myjsonpfunction',
async:'true',
success:function (data) {
//alert("success");
}
});
});
you need write a callback parameter and callback function,the google ajax apis will be return only json if you don't set of callback.
你需要写一个回调参数和回调函数,如果你没有设置回调,google ajax apis将只返回json。
if you set url like this
如果你设置这样的URL
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=?(another)
the response is
反应是
{"responseData": null, "responseDetails": "bad or missing callback or context", "responseStatus": 400}
{“responseData”:null,“responseDetails”:“错误或缺少回调或上下文”,“responseStatus”:400}
#2
2
Looks like the method you are using is deprecated: https://developers.google.com/web-search/docs/reference
您的使用方法看起来已弃用:https://developers.google.com/web-search/docs/reference
And has moved on to: http://code.google.com/apis/customsearch/v1/overview.html
并转到:http://code.google.com/apis/customsearch/v1/overview.html
#1
5
Check this out JsFIddleDemo
看看JsFIddleDemo
/*
* create callbak function for jsonP
* @params
* data is response from http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=myjsonpfunction
*/
function myjsonpfunction(data){
alert(data.responseData.results) //showing results data
$.each(data.responseData.results,function(i,rows){
alert(rows.url); //showing results url
});
}
//request data using jsonP
$(function(){
$.ajax({
url:'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=myjsonpfunction',
type:"GET",
dataType: 'jsonp',
jsonp: 'myjsonpfunction',
async:'true',
success:function (data) {
//alert("success");
}
});
});
you need write a callback parameter and callback function,the google ajax apis will be return only json if you don't set of callback.
你需要写一个回调参数和回调函数,如果你没有设置回调,google ajax apis将只返回json。
if you set url like this
如果你设置这样的URL
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=?(another)
the response is
反应是
{"responseData": null, "responseDetails": "bad or missing callback or context", "responseStatus": 400}
{“responseData”:null,“responseDetails”:“错误或缺少回调或上下文”,“responseStatus”:400}
#2
2
Looks like the method you are using is deprecated: https://developers.google.com/web-search/docs/reference
您的使用方法看起来已弃用:https://developers.google.com/web-search/docs/reference
And has moved on to: http://code.google.com/apis/customsearch/v1/overview.html
并转到:http://code.google.com/apis/customsearch/v1/overview.html