【问题标题】:Expressjs Not Sending Response After POST RequestExpressjs在POST请求后不发送响应
【发布时间】:2018-09-16 13:27:46
【问题描述】:

我在快递方面有这个功能:

// Register user login
exports.register_user = function(req, res) {
    var portalID = req.body.portalID;
    var companyName = req.body.companyName;
    var password = req.body.password;
    var password2 = req.body.password2;

    var salt = bcrypt.genSaltSync(12);
    var hash = bcrypt.hashSync(password, salt);
    password = hash;

    var params = {
        TableName: "HouseAccounts",
        Item: {
            "portalID": portalID,
            "companyName": companyName,
            "points": 0,
            "password": password,
        }
    }
    res.sendStatus(200);
}

这个在前端获取:

function register() {
  fetch("MyURL/register", {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
      "portalID": document.getElementById("portal-id").value,
      "companyName": document.getElementById("company-name").value,
      "password": document.getElementById("password").value,
      "password2": document.getElementById("password2").value
    })
  }).then(function(response){console.log(response)});
}

在快递方面,我可以接收通过 POST 发送的 JSON 并对这些数据进行处理。但是,在我的前端,我没有收到快递方面的回复。连接超时,状态为(failed),控制台出现错误Uncaught (in promise) TypeError: Failed to fetch

【问题讨论】:

  • 这可能之前已经解决了。你试过这个吗? stackoverflow.com/questions/42754388/…
  • @JackJefferies 是的,我已经尝试记录 response.json() 但我根本没有收到任何来自 api 调用的返回。
  • 因为你什么也没发送。只是一个200 响应头。没有从服务器端发送数据
  • 服务器日志中有任何内容吗?没有错误?当您发送res.json(...) 作为测试时会发生什么?
  • @FabianTe 想通了

标签: javascript express


【解决方案1】:

好的,问题是我正在使用 AWS Cloud9 并且我的公共 IP 以访问 API 在重新启动实例时发生了更改,因此请求根本没有到达服务器端。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-12
    • 2017-03-01
    • 2020-07-29
    • 2019-09-29
    • 1970-01-01
    • 2018-07-26
    • 2017-10-12
    • 1970-01-01
    相关资源
    最近更新 更多