Ajax does not turn back any data.
$.ajax({
dataType:'jsonp',
url: url,
async:false,
success: function(data){
getUsername = data.user.id;
},
});
Returning data is null but required to return the userId
Ajax does not turn back any data.
$.ajax({
dataType:'jsonp',
url: url,
async:false,
success: function(data){
getUsername = data.user.id;
},
});
Returning data is null but required to return the userId
Your data is returned correctly :
Object {user: Object, stat: "ok"}
stat: "ok"
user: Object
->id: "66956608@N06"
->username: Object
__proto__: Object
This is how you can process the results :
function foo() {
return $.ajax(...);
}
foo().done(function(result) {
// code depending on result
}).fail(function() {
// an error occurred
});
function getUserId() {
var url = "http://api.flickr.com/services/rest/?jsoncallback=?&api_key=fc6c52ed4f458bd9ee506912a860e466&method=flickr.urls.lookupUser&format=json&nojsoncallback=1&url=http://www.flickr.com/photos/flickr";
var getUsername = null;
return $.ajax({
dataType: 'jsonp',
url: url,
async: false
});
}
getUserId().done(function (result) {
// Call the alert here..
alert(result.user.id);
}).fail(function(err){
alert('an error has occured :'+err.toString());
});
You need to do this:
function getUserId() {
var url = "http://api.flickr.com/services/rest/?jsoncallback=?&api_key=fc6c52ed4f458bd9ee506912a860e466&method=flickr.urls.lookupUser&format=json&nojsoncallback=1&url=http://www.flickr.com/photos/flickr";
var getUsername = null;
return $.ajax({
dataType: 'jsonp',
url: url,
async: false
});
}
getUserId().done(function (result) {
// Call the alert here..
alert(result.user.id);
});
This is because the AJAX function is called asyncronously. This means that the URL is called, and while the processing is taking place, the javascript code continues executing and returns from your function. You can see this by putting an alert for getUsername inside of your success handler. At that point you can see that the data is actually being returned correctly.