jQuery.ajax()到json url返回一个字符串而不是一个json对象

时间:2022-10-07 16:55:42

I'm having a problem where the data object passed to my complete() callback function is not a json object, but rather is an [Object object]. I can see a string of my json response in data.responseText.

我遇到的问题是传递给我的complete()回调函数的数据对象不是json对象,而是[Object object]。我可以在data.responseText中看到我的json响应字符串。

Here is my jQuery .ajax request:

这是我的jQuery .ajax请求:

$.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 response that is being sent:

以下是发送的响应:

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

It evaluates to valid JSON, and I am sending the correct json content-type headers from the server. Stumped on this one!

它评估为有效的JSON,我从服务器发送正确的json内容类型头。难倒在这一个!

1 个解决方案

#1


3  

The complete callback's signature is complete(jqXHR, textStatus), jqXHR gives you [Object object].

完整的回调签名完成(jqXHR,textStatus),jqXHR为您提供[Object object]。

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

相反,你应该使用成功(data,textStatus,jqXHR)回调,如果请求成功将被调用,这次数据将为你提供正确的东西。

More info please check the manual.

更多信息请查看手册。

#1


3  

The complete callback's signature is complete(jqXHR, textStatus), jqXHR gives you [Object object].

完整的回调签名完成(jqXHR,textStatus),jqXHR为您提供[Object object]。

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

相反,你应该使用成功(data,textStatus,jqXHR)回调,如果请求成功将被调用,这次数据将为你提供正确的东西。

More info please check the manual.

更多信息请查看手册。