How retrieve responseJSON property of a jquery $.ajax object

ajax, javascript, jquery, json

Solution

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.

Problem

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:

Original source

Related problems