简体   繁体   English

通过Ajax请求发送后无法解析JSON

[英]Can't parse JSON after sending it via Ajax request

Client side 客户端

After applying JSON.stringy to an object it is sent like this to a node-server (as POST-request): JSON.stringy应用于对象后,它会像这样发送到节点服务器(作为POST请求):

{"id":"topFolder","parentPath":null,"name":"newProject","is":"root","children":[]}

I'm sending the request on the client via Polymer's iron-ajax-element: <iron-ajax id="ajaxSave" method="POST" url="/save" handle-as="json" on-response="doit" </iron-ajax> 我通过Polymer的iron-ajax-element在客户端发送请求: <iron-ajax id="ajaxSave" method="POST" url="/save" handle-as="json" on-response="doit" </iron-ajax>

It is sent with this function: 它与此功能一起发送:

save: function() {
  var v = JSON.stringify(this.data);
  this.$.ajaxSave.body = v;
  this.$.ajaxSave.generateRequest();
}

Server side 服务器端

Then I try to JSON.parse the request's body on the Koa-server (using Koa-Body as the body parser): 然后我尝试在Koa服务器上JSON.parse请求的主体(使用Koa-Body作为主体解析器):

router.post('/save', body, function*(){
  let data = JSON.parse(this.request.body);
  this.body = "all ok";
})

I get a SyntaxError: Unexpected token o and the raw body looks like this: 我得到一个SyntaxError: Unexpected token o ,原始主体看起来像这样:

{ '{"id":"topFolder","parentPath":null,"name":"new Project","is":"root","children":': [ '' ] }

Why does the received body look different and how can I fix it? 为什么收到的身体看起来不同,我该如何解决?

Edit: This is a full curl-command of the request: 编辑:这是请求的完整curl命令:

curl ' http://localhost:3000/save ' -H 'Authorization: Basic YWRtaW46ZXZvbGE=' -H 'Origin: http://localhost:3000 ' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36' -H 'content-type: application/x-www-form-urlencoded' -H 'accept: application/json' -H 'Referer: http://localhost:3000/ ' -H 'Cookie: ajs_anonymous_id=%22e43155da-6541-45de-af9f-046ff5ac7b3c%22; curl'http :// localhost:3000 / save'-H'授权:基本YWRtaW46ZXZvbGE =' - H'来源: http:// localhost: 3000'-H'接受编码:gzip,deflate'-H'接受 -语言:de-DE,de; q = 0.8,en-US; q = 0.6,en; q = 0.4'-H'用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10_11_2)AppleWebKit / 537.36(KHTML ,像Gecko)Chrome / 46.0.2490.80 Safari / 537.36'-H'内容类型:application / x-www-form-urlencoded'-H'accept:application / json'-H'Referer: http:// localhost: 3000 / '-H'Cookie:ajs_anonymous_id =%22e43155da-6541-45de-af9f-046ff5ac7b3c%22; currentUserId=34; currentUserId = 34; -H 'Connection: keep-alive' --data '{"id":"topFolder","open":false,"parentPath":null,"name":"new Project","children":[]}' --compressed -H'连接:keep-alive'--data'{“id”:“topFolder”,“open”:false,“parentPath”:null,“name”:“new Project”,“children”:[]} ' - 压缩

Your object is already an object. 您的对象已经是一个对象。

JSON.parse() is used to convert a string containing JSON notation into a Javascript object. JSON.parse()用于将包含JSON表示法的字符串转换为Javascript对象。

For example, try: this.request.body["id"] to get one of properties. 例如,尝试: this.request.body["id"]获取其中一个属性。

The problem was that the content-type was set to application/x-www-form-urlencoded . 问题是content-type设置为application/x-www-form-urlencoded It has to be application/json 它必须是application/json

Usually body is not a string, so try to explicitly turn it into one. 通常,body不是字符串,因此请尝试将其明确地转换为一个字符串。

body.toString()

If it doesn't help... In some transport libraries you have to read body prior to use it by calling body.read(). 如果它没有帮助......在某些传输库中,您必须在使用它之前通过调用body.read()来读取body。 Read operation can be async.. 读操作可以是异步的..

With q-io it looks like 用q-io看起来像

request(someURL)
.then( function(res) {
  return res.body.read() 
})
.then( function (body){
  obj = JSON.parse(body.toString()
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM