【问题标题】:ajax response with node js使用节点 js 的 ajax 响应
【发布时间】:2021-01-03 14:59:22
【问题描述】:

我无法在 ajax 中得到响应。我知道我走的是正确的路线,因为它正在响应后端的正确日志。但我无法得到 res.end 工作。我尝试了其他方法以及来自这篇文章的示例。 How to return success from ajax post in Node.js.

    $.ajax({
      url: "/register",
      method: "POST",
      async: false,
      dataType: "json",
      data: { email: email, type: "checkCount" },
      success: function (result) {
        console.log(result.status + " status of result"); // does not display result...
      },
      error: function (xhr, status, error) {
        var errorMessage = xhr.status + ": " + xhr.statusText;
        console.log(errorMessage);
      },
    });

////////////////////////////////////////////////////////////

var express = require("express");
var router = express.Router();
var postFunctions = require("../public/postFunctions/register");

router
  .route("/")
  .get(function (req, res) {
    res.render("register");
  })
  //@param dm = decisionMaker for post -- dm = req.body.type -- type in switch -- response made
  .post(function (req, res) {
    let dm = req.body.type;
    switch (dm) {
      case "registerForm":
        res.send("uploadData");
        break;
      case "checkCount":
        console.log("should be an error message"); // this displays fine so i know route works
        res.end('{"success" : "Updated Successfully", "status" : 200}'); //not sure why not showing in ajax result
        break;
      default:
        res.send("action failed. type not found");
    }
  });

module.exports = router;

【问题讨论】:

    标签: javascript node.js ajax


    【解决方案1】:

    应该是 res.send 你用过 res.end 。请使用正确的语法。 res.end() 将结束响应过程。

    【讨论】:

    • res.send({"success" : "更新成功", "status" : 200});
    • 我只是没有在浏览器上激活日志记录...
    猜你喜欢
    • 2017-09-24
    • 2013-06-08
    • 1970-01-01
    • 2020-09-08
    • 2015-08-19
    • 2012-10-04
    • 1970-01-01
    • 2017-11-28
    • 2018-12-29
    相关资源
    最近更新 更多