鍍金池/ 問答/Java  C++  HTML/ node.js request post json數(shù)組中文亂碼問題

node.js request post json數(shù)組中文亂碼問題

var request = require('request')
var url = '' 
request({
    url:url,
    method:'POST',
    json:true,
    headers:{
        'content-type':'application/json',
    },
    body:JSON.stringify({
        body:
          {
            content:"商務(wù)",
            visitor_name:"張三",      
            visitor_company_name:"淘寶",
            check_in_plcae:"深圳",
            visitor_type:"01",
            host:"03",
            visitor_num:"1",
            photo:""
          }
    }),function (error,response,body) {
        if(!error && response.statusCode == 200){
            console.log(body);
        }
        
    }
})

麻煩各位大佬幫我看看為什么這樣發(fā)送出去,對方收到的是中文亂碼

"{\"body\":{\"content\":\"???\",\"visitor_name\":\"é????\",\"visitor_company_name\":\"????\",\"check_in_plcae\":\"??±??\",\"visitor_type\":\"01\",\"host\":\"???°01\",\"visitor_num\":\"1\",\"photo\":\"\"}}"

如何解決node.js request這個模塊發(fā)送post出現(xiàn)中文亂碼的問題

回答
編輯回答
舊顏
const http = require('http');
var querystring = require('querystring');
const postData = JSON.stringify(
    {
        "body":
          {
            "content":"拜訪",
            "visitor_name":"李四",
            "visitor_company_name":"",
            "check_in_plcae":"南京",
            "visitor_type":"02",
            "host":"01",
            "visitor_num":"11",
            "photo":""
          }
      }
  );
  console.log(postData);
  
  const options = {
    hostname: '',
    port: 8089,
    path: '',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': Buffer.byteLength(postData)
    }
  };
  
  const req = http.request(options, (res) => {
    console.log(`狀態(tài)碼: ${res.statusCode}`);
    console.log(`響應(yīng)頭: ${JSON.stringify(res.headers)}`);
    res.setEncoding('utf8');
    res.on('data', (chunk) => {
      console.log(`響應(yīng)主體: ${chunk}`);
    });
    res.on('end', () => {
      console.log('響應(yīng)中已無數(shù)據(jù)。');
    });
  });
  
  req.on('error', (e) => {
    console.error(`請求遇到問題: ${e.message}`);
  });
  
  // 寫入數(shù)據(jù)到請求主體
  req.write(postData);
  req.end();

最后我通過這種方式發(fā)送了過去

2018年7月31日 23:32