【发布时间】:2018-10-16 17:16:23
【问题描述】:
我正在使用http 模块向 api 发送请求。所以我的响应体非常大,而且我变得不完整,并且在尝试解析为 javascript 对象时出现错误,即 json 无效。
这是我的代码。
function sendPostRequest(method, url, data, callback) {
if (typeof data === 'undefined') {
data = {};
}
var data = querystring.stringify(data);
var post_options = {
host: API.Host,
port: API.Port,
path: API.Prefix + url,
method: method,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + API_USER.token
}
};
var post_req = http.request(post_options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
callback(chunk);
});
});
// post the data
post_req.write(data);
post_req.end();
}
sendPostRequest('GET', 'user/get_accounts', data, function (res) {
res = JSON.parse(res);
mainWindow.webContents.send('user:account', res);
return;
}, true);
请帮忙解决这个问题!谢谢!
【问题讨论】: