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 version
http://code.jquery.com/jquery-1.6.2.min.js
to
http://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