尝试访问 stackoverflow api 但出现解析错误
在尝试将 stackoverflow api 与 ajax 和 jquery 一起使用时,我无法让它工作。我知道我必须使用 jsonp 作为数据类型,并且我一直在阅读执行 jsonp 请求的不同方法,但我仍然无法让它工作。
这是我的ajax请求:
var API_KEY = "XXXXXXXXXXXX";
var URL = "http://api.stackoverflow.com/1.1/";
var _url = URL + 'users?filter=locrizak';
$.ajax({
dataType:'jsonp',
jsonp : false,
jsonpCallback : "onJsonp",
url: _url,
success: function(val) {
console.log('success');
//console.log(val);
},
error: function(val) {
console.log('error');
console.log(arguments);
}
});
function onJsonp() {
console.log(arguments);
};
无论我尝试什么,我总是在firebug中得到这个响应:
"parsererror" "onJsonp was not called"
我知道我正在做一些非常愚蠢的事情,因为我遇到了同样的问题尝试使用 Twitter api 时遇到问题,但我一辈子都不记得我做了什么才能让它工作。
更新
所以我看了@genesis 的工作演示,并尝试了几次不同的方法,但没有运气。然后我注意到我的 jQuery 版本并将其切换到他正在使用的版本,它神奇地工作了。
我改了最新版本 http://code.jquery.com/jquery-1.6.2.min.js
到 http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js
嗯不知道为什么它会起作用,可能是一个错误,但谁知道也许其他东西发生了变化。
如果有人知道为什么,如果你能解释一下那就太好了。我还意识到 jQuery 自动添加回调,但我无法让它像那样工作。我能做些什么来让它工作,我想你会说“更合适”的方式?
var URL = "http://api.stackoverflow.com/1.1/";
api.get(URL + 'users?filter=locrizak');
api.get = function(url) {
$.ajax({
/*dataType:'jsonp',*/
dataType:'json',
jsonp : false,
url: url + '&jsonp=api.onJsonp',
success: function(val) {
console.log('success');
//console.log(val);
},
error: function(val) {
//error gets called but.......
console.log(arguments);
console.log('error');
console.log(val);
}
});
};
api.onJsonp = function() {
//so does the callback!!
console.log('called');
console.log(arguments);
}
//note this code is simplified
In trying to use the stackoverflow api with ajax and jquery and I just can't get it to work. I know I have to use jsonp
as the datatype and I keep reading different ways of doing the jsonp request but I still can't get it to work.
This is my ajax request:
var API_KEY = "XXXXXXXXXXXX";
var URL = "http://api.stackoverflow.com/1.1/";
var _url = URL + 'users?filter=locrizak';
$.ajax({
dataType:'jsonp',
jsonp : false,
jsonpCallback : "onJsonp",
url: _url,
success: function(val) {
console.log('success');
//console.log(val);
},
error: function(val) {
console.log('error');
console.log(arguments);
}
});
function onJsonp() {
console.log(arguments);
};
No matter what I try I always get this response in firebug:
"parsererror" "onJsonp was not called"
I know that I am doing something really dumb because I ran into the same issues when trying to use the Twitter api but I can't for the life of me remember what I did to get it to work.
Update
So I took a loog @genesis's working demo and tried it a few time and different ways but no luck. Then I notice my jQuery version and switched it to the one he was using and it magically worked.
I change the most recent versionhttp://code.jquery.com/jquery-1.6.2.min.js
tohttp://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js
hmm no idea why it works, possibly a bug but who knows maybe something else changed.
If anyone knows why it would be awesome if you could explain. Also I realize that jQuery adds the callback automatically but I could not get it working like that. What could I do to get this working, I guess you would say a "more proper" way?
var URL = "http://api.stackoverflow.com/1.1/";
api.get(URL + 'users?filter=locrizak');
api.get = function(url) {
$.ajax({
/*dataType:'jsonp',*/
dataType:'json',
jsonp : false,
url: url + '&jsonp=api.onJsonp',
success: function(val) {
console.log('success');
//console.log(val);
},
error: function(val) {
//error gets called but.......
console.log(arguments);
console.log('error');
console.log(val);
}
});
};
api.onJsonp = function() {
//so does the callback!!
console.log('called');
console.log(arguments);
}
//note this code is simplified
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须致电
http://api.stackoverflow.com/1.1/users?filter=locrizak& jsonp=jsonp
工作演示和代码:
You have to call
http://api.stackoverflow.com/1.1/users?filter=locrizak&jsonp=jsonp
working demo and code:
您可以将
jsonp:'jsonp',
添加到ajax()
调用。 stackoverflow API 文档指出它需要jsonp
查询参数和 jQuery 属性来配置传递的查询字符串参数称为 jsonp。如果没有它,默认情况下会将callback=?
添加到 URL 末尾。我在控制台中获得
成功
,运行以下命令:此外,
jsonp:false,
不应该不设置为false
;它需要为true
,否则不会添加查询字符串参数。更新:让它与 jQuery v1.6.2 一起正常工作,并使用正确的参数,如我上面的原始答案中所述。回调函数必须位于 jQuery 匿名函数之外。
更新 2: 根据评论,这是另一个版本,这次包装在一个对象中。请注意,您不能使用 jsonpCallback: 'api.onJsonp', 因为它仅在 jQuery 匿名函数内定义。保持封装的最简单方法是创建一个全局函数,然后将控制权传递回相应的
api
函数。You can add
jsonp:'jsonp',
to theajax()
call. The stackoverflow API documentation states that it needs thejsonp
query paramater and the jQuery property to configure the query string parameter passed is calledjsonp
. Without it, the default is to addcallback=?
to the end of the URL.I get
success
in the console, running this:Also,
jsonp:false,
should not be set tofalse
; it needs to betrue
otherwise no query string parameter is added.Update: Got it working correctly with jQuery v1.6.2 and using the correct parameters as described in my original answer above. The callback function must be outside the jQuery anonymous function.
Update 2: Based on comments, here is another version, this time wrapped in an object. Note that you cannot use
jsonpCallback: 'api.onJsonp',
because that is only defined inside the jQuery anonymous function. The simplest way to keep the encapsulation is to create a global function and just pass control back to theapi
counterpart.onJsonp 是在闭包中定义的吗?如果是这样,也许您通过名称(“onJsonp”)而不是通过引用引用回调的事实是问题所在?
两种解决方案:要么在全局范围内定义 onJsonp,要么通过删除引号来通过引用传递它。
Is onJsonp defined within a closure? If so, perhaps the fact you're referring to the callback by name ('onJsonp') instead of by reference is the problem?
Two solutions: either define onJsonp in global scope, or pass it by reference by removing the quotes.
将 jsonp 设置为 true。
set jsonp to true.