尝试使用 NodeJS 发出 get 请求

Trying to make a get request using NodeJS

本文关键字:发出 get 请求 NodeJS      更新时间:2023-09-26

我正在尝试向我的 NodeJS 服务器端发出 get 请求,但它一直说CERT_UNTRUSTED。我在客户端使用的代码是:

function validateUser(){
  var userEmail = $("#userEmail").val();
  var userPassword = $("#userPassword").val();
  $.ajax({
    url: "https://changelog.twvending.net/ValidateUserByName?userName="+userEmail+"&userPassword="+userPassword,
    success: function(user){
        if (user === "bad user"){
            alert("Your credentials are incorrect, please try again");
        } else {            
            if (user.User.PermissionLevel === 3){
                localStorage.setItem("permission", "hello");
                    $("#newEntry").show();
                    window.location.href = "https://changelog.twvending.net";
            }
            else {
                alert("You do not have permission to make new entries");
                window.location.href = "https://changelog.twvending.net";
            }
        }
    }, 
    error: function(xhr,status,error){
    }
  });
}

在服务器端

app.get('/ValidateUserByName', function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
  var userEmail = req.query.userName;
  var userPassword = req.query.userPassword;
  processInput(userEmail +" logged in");
    var request = require('request');
    request.get('https://32market.com/32marketpcitest/threesquaremarketrest.svc/ValidateUserByEmailULR?email='+userEmail+'&password='+userPassword,
   function (err, resp, body) {
       if (err){
           processInput ( err ) 
           console.log('Error: ' + err);
           return;
       }
       try {
            var user = JSON.parse(body); 
            res.send(user);
        }
        catch(err) {
            processInput('error logging in: invalid user name or password');
            res.send("bad user");
        }  
   });
});

我的问题是我错过了什么客户或服务器不喜欢。我还应该指出,这是我第一次做这样的项目。

我的猜测是您没有所需的或过期的 SSL 证书来执行您的请求。可能要32market.com它是否使用 https 连接?这是您自己的网站吗?

您可以尝试添加

  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; 

在您提出请求之前

但这并不完全安全。