JQuery.ajax () in json url returns a string instead of a json object

I had a problem when the data object passed to my full () callback function is not a json object, but rather a [Object Object]. I see a json response line in the data.responseText file.

Here is my jQuery.ajax request:

$.ajax({ url: 'api.php', dataType: 'json', data: { command: "GetBlacklist" }, type: 'POST', error: function(jqXHR, textStatus, errorThrown) { messageDiv.append("Error: " + errorThrown + "<br />"); }, complete: function(json) { $('.blacklist textarea').text(json.message); messageDiv.append("Blacklist loaded."); } }); 

And here is the answer that is sent:

 {"message":"success","result":0} 

It evaluates the valid JSON and I am posting the correct json header headers on the server. It’s worth it!

+4
source share
1 answer

The complete signature of the complete callback complete(jqXHR, textStatus) , jqXHR gives you [Object Object].

Instead, you should use the success(data, textStatus, jqXHR) callback success(data, textStatus, jqXHR) , which will be called if the request is successful, and this time data will give you the right thing.

For more information, please check the manual .

+3
source

All Articles