简体   繁体   English

HTTP Post请求json数据更改

[英]HTTP Post request json data changes

For some weird reason, when I have my client send an HTTP Post request to my server, the data changes. 出于某种奇怪的原因,当我的客户端向我的服务器发送HTTP Post请求时,数据会发生变化。 Everytime I console.log the data in server, my data no longer has commas. 每次我在server.log服务器中记录数据时,我的数据都不再有逗号。 Instead, it replaces every single comma with &. 相反,它用&替换每个逗号。 Why are all my commas changing to the & symbols and how do I fix this? 为什么我的所有逗号都改为&符号,我该如何解决?

Client: 客户:

var request = require('request');
request.post('http://11.11.11.111:9000/server.js', {form:{ "id" : "42", "helpme" : "no"}})  

Server: 服务器:

req.on('data', function(chunk) {
     //file1=chunk;
     file1=JSON.parse(chunk);
     console.log(file1);
}

Instead of using the form option for request , use the json option: 不使用form选项进行request ,而是使用json选项:

request.post('http://11.11.11.111:9000/server.js', {
  json : { "id" : "42", "helpme" : "no"}
})

Also, be aware that multiple data events can be generated, each containing incomplete data. 另外,请注意可以生成多个data事件,每个事件都包含不完整的数据。 You need to store the incoming data and parse it at the end: 您需要存储传入的数据并在最后解析它:

var chunks = [];
req.on('data', function(chunk) {
  chunks.push(chunk);
});
req.on('end', function() {
  var data = JSON.parse( Buffer.concat(chunks) );
  ...
});

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

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