jQuery的$。获得内部没有解析JSON响应 [英] Jquery $.get is not internally parsing json response

查看:123
本文介绍了jQuery的$。获得内部没有解析JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $。获得(API /套,函数(结果){
    的console.log(套房获取API的结果是一个类型的 - + typeof运算结果)
    的console.log(定制套件的列表是+结果)
  },JSON)
 

它打印,

 套房获取API的结果是类型 - 字符串
  定制套件的列表是{\套房\:\H \,\B \]}
 

,卷曲检查响应头 -

 卷曲-v -X GET的http://本地主机:8002 / API /套
 

节目,

 内容类型:应用程序/ JSON;字符集= UTF-8
{\套房\:\H \,\B \]}
 

为什么不$不用彷徨转换JSON数据转换成一个对象,调用回调函数之前?

我试过同Ajax调用,而不是$不用彷徨,但还是回调得到的字符串。

  $。阿贾克斯({URL:API /套,法:GET,数据类型:JSON,头:{接受:应用程序/ JSON;字符集= UTF-8}})
 

解决方案

现在的问题是,响应具有额外的引号包装对象的JSON再presentation ...使其 JSON在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")

It prints,

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

Check response headers with curl -

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

shows,

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

Why doesn't $.get convert the json data into an object, before calling the callback function?

I tried the same with ajax call instead of $.get, but still the callback gets a string.

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

解决方案

The problem is that the response has extra quotes wrapping the json representation of the object...making it json in json string (for lack of a better description)

Remove the extra quotes at server that are wrapping the outer {} so that it is valid to convert to object

这篇关于jQuery的$。获得内部没有解析JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆