nodejs https api请求错误错误:在TLSWrap.onread(net.js:568:26)的exports._errnoException(util.js:1018:11)处读取ECONNRESET

时间:2017-08-30 14:05:38

标签: node.js ajax api econnreset

我正在将此转换为ajax' POST'到节点,无法通过此错误消息。我已经查看了其他类似How to avoid a NodeJS ECONNRESET error?的帖子,但似乎并不适用。我发布标题基本身份验证并获取令牌。有人知道或者之前遇到过这个问题吗?

错误消息:

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);
        });
    });

0 个答案:

没有答案