我有一美元.数据类型设置为"json"的ajax()请求服务器返回的JSON具有正确的mime类型"application/JSON"然而,jqXHR对象中的responseText始终是一个字符串.我做错了什么?应该是这样的吗?
以下是我打电话的方式:
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 I have have to do a $.parseJSON(jqXHRObject.responseText)
to get an actual object. This seems unnecessary as $.ajax() should be automatically converting responseText according to the docs. Thanks!