【问题标题】:Node app.get not found when trying to make a post request towards it, but works in browser尝试向其发出发布请求时未找到节点 app.get,但可在浏览器中使用
【发布时间】:2019-10-17 22:26:45
【问题描述】:

我在 Nodejs 中创建了一个用于登录的 API。当我尝试对其进行 jquery 发布请求时,它显示 404 Not found,但是当我在浏览器中打开同一页面时,它会找到该文件。

我要请求的代码:

$.post("http://localhost:8000/api/login", {"username": "username", "password": "password"}, function(response) {

    console.log(response);

});

我的 API 代码(简化):

app.get("/api/login", function(req, res) {

  var username = req.body.username,
  password = req.body.password;

  //login chechks but basically this is what it returns

  res.send({

    "error": 0,
    "loggedIn": 1

  });
  res.end();

});

【问题讨论】:

    标签: jquery node.js post


    【解决方案1】:

    app.get 创建一个接受 GET 请求的路由。 将其更改为 app.post 以接受 POST 请求。

    https://expressjs.com/en/4x/api.html#app.get.method

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-15
      • 2020-03-29
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      相关资源
      最近更新 更多