ajax - Why is jqXHR.responseText returning a string instead of a JSON object? -


i have $.ajax() request datatype set "json." server returning json correct mime type of "application/json." , yet responsetext in jqxhr object string. doing wrong? how it's supposed work?

here's how i'm making call:

var options = {      datatype:'json',     type: 'get',     url: "http://example.com/api/" };  var key = "passtocallback";  var jqxhrobject =  $.ajax(options).then(     function(data, textstatus, jqxhr, key) {         this.success(data, textstatus, jqxhr, key);     },     function(jqxhr, textstatus, errorthrown) {          this.error(jqxhr, textstatus, errorthrown);     } );  console.log(jqxhrobject.getresponseheader("content-type")); // application/json console.log(typeof jqxhrobject.responsetext); // string 

so have have $.parsejson(jqxhrobject.responsetext) actual object. seems unnecessary $.ajax() should automatically converting responsetext according docs. thanks!

i had same problem. returns string because formulated exception. e.g. use kernel listener serialization json on symfony2 project. correct proper rest headers.

anyway, parse it; works me:

$.ajaxsetup({     "error": function(jqxhr, status, thrownerror) {         alert('error');         var responsetext = jquery.parsejson(jqxhr.responsetext);         console.log(responsetext);     } }); 

Comments