Jquery美元.Get不在内部解析json响应


  $.get("api/suites", function(result){
    console.log("the suites get api result is of type - " + typeof result)
    console.log("the list of custom suites are " + result)
  }, "json")

打印,

  the suites get api result is of type - string
  the list of custom suites are "{"suites":["h","b"]}"

用curl检查响应头-

curl -v -X GET http://localhost:8002/api/suites

,

Content-Type: application/json; charset=utf-8
"{"suites":["h","b"]}"

为什么$。获取转换json数据成一个对象,调用回调函数之前?

我尝试了同样的ajax调用而不是$。获取,但回调函数仍然获取一个字符串。

  $.ajax({"url": "api/suites", "method": "GET", "dataType": "json", headers: {     Accept : "application/json; charset=utf-8" }})

问题是响应有额外的引号包装对象的json表示…使其成为json in json字符串(由于缺乏更好的描述)

删除服务器上包装外部{}的额外引号,以便有效转换为对象

我认为它不会自动做到这一点。不确定你是否正在调用你自己的API,但你请求的API正在以字符串格式返回响应。下面的代码应该可以工作:

result = JSON.parse(result)

最新更新