中文乱码求解
发布于 9 年前 作者 xieyijun 4561 次浏览 最后一次编辑是 8 年前 来自 问答

如下要使用post请求,参数类型是JSON,但是里面包含有中文 “提单状态通知”,所以一直有乱码 '�ᵥ״̬֪ͨ ,用querystring.stringify好像不支持多级的JSON,只能一级而已,否则POST DATA里的DATA就会丢掉。

var postData = { “touser”: openid, “template_id”: “W_EW3DG3rp1IcH2lQm6IBJtteSIx6ZigabPQXzG4_dk”, “url”: “”, “data”: { “first”: { “value”: “提单状态通知”, “color”: “#173177” }, “keyword1”: { “value”: “HBL201510010001”, “color”: “#173177” }, “keyword2”: { “value”: “ETA”, “color”: “#173177” }, “remark”: { “value”: “2015-10-01”, “color”: “#173177” } } }; console.dir(postData); postData = JSON.stringify(postData);

var options = {
    host: 'api.weixin.qq.com',
    path: '/cgi-bin/message/template/send?access_token=' + accesstoken,
    method: 'POST',
    headers: {
        'Content-Type': 'application/json; charset=utf-8',
        'Content-Length': Buffer.byteLength(postData)
    }
};

var req = https.request(options, function (res) {
    //console.log("statusCode: ", res.statusCode);
    //console.log("headers: ", res.headers);
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        var body = JSON.parse(chunk);
        console.dir(body);
    });
});
console.dir(JSON.parse(postData));
req.write(postData);
req.end();
1 回复

已经解决~

回到顶部