【问题标题】:jquery $.ajax gives 404 not found. Im using node.js with expressjquery $.ajax 给出 404 未找到。我使用 node.js 和 express
【发布时间】:2016-11-05 15:26:46
【问题描述】:

这个jquery:

  $.ajax({
    method: "post",
    data: JSON.stringify(data),
    contentType: "application/json",
    url: "http://localhost:3000/ajax"
  });

给出错误 404 未找到。这是我的服务器端:

router.get('/ajax', function(req, res ,ext){
    var strings = ["rad", "bla", "ska"]

    console.log('body: ' + JSON.stringify(req.body));
    console.log("AJAX RECEIVED");
    res.send(strings);
});

所以我确实有 /ajax 路线。当我转到http://localhost:3000/ajax 时,我能够访问该站点。但是,当我尝试使用 ajax 方法访问它时,出现 404 错误。所以我想知道错误是否与我的代码有关,或者是否可能是我计算机上的防火墙。我实际上使用的公司计算机具有阻止某些站点的防火墙,我无法禁用它。

【问题讨论】:

  • 已解决。它使用的是 router.post 而不是 get。谢谢大家。

标签: javascript jquery ajax node.js express


【解决方案1】:

如果您想进行 HTTP POST,请使用 router.post,而不是 router.get

【讨论】:

    【解决方案2】:

    您的 $.ajax 呼叫正在使用 POST 但您在 express.js 中的路由仅侦听 GET 请求。

    当您通过浏览器点击 URL 时,它正在执行 GET,所以它起作用了。

    您需要使用router.post('/ajax', ...) 为POST 设置路由

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      相关资源
      最近更新 更多