我正在将 ajax 'POST' 下面的这个转换为 Node 并且无法通过这个错误消息。我看过其他类似的线程如何避免 NodeJS ECONNRESET 错误?但似乎不适用。我正在发布标头基本身份验证并取回令牌。有人知道或以前遇到过这个问题吗?
错误信息:
Error: read ECONNRESET at exports._errnoException (util.js:1018:11) at TLSWrap.onread (net.js:568:26)
下面是有效的 ajax 调用:
function createAuth(user, pass) {
var token = user + ":" + pass;
var hash = btoa(token);
return "Basic " + hash;
}
var username = "username";
var password = "password";
$.ajax({
type: "POST",
url: "https://apiserviceprovider/api/login",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", createAuth(username, password));
},
success: function(data) {
sessionStorage.setItem("mytoken", data.token);
},
});
下面是Nodejs代码:
var user = 'username';
var pass = 'password';
https = require("https"),
https_options = {
"host": "apiserviceprovider",
"path": "/api/login",
"port": 443,
"method": "POST",
"headers": {
"Authorization": createAuth(user, pass)
}
},
request = https.request(https_options, function(response) {
console.log('Request...');
console.log('Status: ' + response.statusCode);
console.log('Headers: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function(chunck) {
console.log('BODY: ' + chunk);
});
request.on('end', function() {
console.log('problem with request: ' + chunk);
});
request.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
});