问题描述
我知道这已经被问了无数次,但我还是不能让我的code工作。我想从我的Javascript应用程序的简单JSONP调用。鳕鱼的片段是这样的:
<$p$p><$c$c>url="http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=?";和然后:
$。的getJSON(URL,功能(数据){
警报(你好1');
});
或
$阿贾克斯(网址:{url:URL,
数据类型:JSONP,
成功:功能(数据){警报(你好2); },
错误:函数(J,T,E){警报(T);}
});
这两种方法的工作原理。第二种方法导致的错误的警报。第一个不返回任何成功。我究竟做错了什么?很多很多的感谢!
更新:我想我至少发现一个问题。让我看起来更到这一点。
更新2:对不起,这个code的实际工作,至少第一种方法。与会者普遍认为,导致code不工作这code段围绕一个细微的错误,但总体而言,这工作得很好。异步调用有时是有点棘手: - )
检查了这一点 JsFIddleDemo
/ *
*创建callbak功能JSONP
* @params
*数据是从http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=myjsonpfunction响应
* /
函数myjsonpfunction(数据){
警报(data.responseData.results)//显示结果数据
$每个(data.responseData.results,功能(我行){
警报(rows.url); //显示结果网址
});
}
使用JSONP //请求数据
$(函数(){
$阿贾克斯({
url:'http://ajax.googleapis.com/ajax/services/search/webv=1.0&q=AAA&callback=myjsonpfunction',
键入:GET,
数据类型:JSONP,
JSONP:myjsonpfunction,
异步:真,
成功:功能(数据){
//警报(成功);
}
});
});
您需要编写一个回调参数和回调函数,在谷歌AJAX API的将是,如果你不设置回调只返回JSON。
如果您设置的URL像这样
<$p$p><$c$c>http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=AAA&callback=?(another)响应是
{responseData:空,responseDetails:坏或丢失回调或上下文,responseStatus:400}
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:
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 :-)
Check this out 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/webv=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.
if you set url like this
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}
这篇关于通过JQuery JSONP调用谷歌AJAX搜索API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!