小程序订阅消息。在服务端调用HTTPS接口。服务端用的是node。如果发送的数据里不包含中文就没问题,但是有中文就会报这个错。
以下是代码:var content = JSON.stringify({
"touser": "oYrCr5W_D9bx5HK8ax2g6h11I0_s",
"template_id": "yBwuYTllkNb8Zrun02L3hky23ThLoO8Ppqcnsul1ZHw",
"page": "index",
"miniprogram_state": "developer",
"lang": "zh_CN",
"data": {
"thing1": {
"value": "吼吼"
},
"time3": {
"value": "2015-01-05"
},
"name2": {
"value": "TIT"
},
"thing4": {
"value": "123456"
},
"thing5": {
"value": "123456"
}
}
});
var options = {
host: 'api.weixin.qq.com',
path: '/cgi-bin/message/subscribe/send?access_token=' + wxtoken,
method: 'POST',
timeout: 10000,
headers: {
'content-type': 'application/json',
'Accept': 'application/json',
// 'Content-Type': 'text/html; charset=utf-8',
'Content-Length': content.length
}
};
try {
var req = https.request(options, function (res) {
var _data = '';
res.on('data', function (chunk) {
_data += chunk;
});
res.on('end', function () {
// console.log("operWXMessage result222: ", wxtoken);wxtoken
console.log("operWXMessage result:", _data);
});
});
req.write(content);
req.end();
} catch (err) {
console.log("operWXMessage throw err ", err);
}
}
我的也是汉字的问题,去掉汉字就可以发消息了。折腾47001一天多了,看到这个帖子提醒了我,谢谢。后续再试试汉字的编码问题。postman可以发,程序按理也能发:)
发送http请求给微信的时候,注意要用UTF-8编码