Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  159] [ 4]  / answers: 1 / hits: 91072  / 10 Years ago, thu, may 15, 2014, 12:00:00

I have this javascript:



$ajax = $.ajax({
type: 'GET',
url: 'DBConnect.php',
data: '',
dataType: 'json',
success: function(data) {},
error:function (xhr, ajaxOptions, thrownError) {
dir(thrownError);
dir(xhr);
dir(ajaxOptions);
}
});
console.dir($ajax);
console.dir($ajax.responseJSON);


console.dir($ajax) shows it has a property named responseJSON, but when I try to access it with $ajax.responseJSON it returns undefined:
enter


More From » jquery

 Answers
13

Well, of course it's undefined, because at the moment when you run console at last lines of your code, response hasn't yet came from the server.



$.ajax returns promise, which you can use to attach done() and fail() callbacks, where you can use all the properties that you see. And you have actually used callback error and success, and that's where you can run code and other functions that rely on data in the response.


[#70991] Wednesday, May 14, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucillemariselal

Total Points: 108
Total Questions: 97
Total Answers: 119

Location: Thailand
Member since Thu, May 6, 2021
3 Years ago
;