javascript - 使用节点 js 的 ajax 响应
问题描述
我无法在 ajax 中得到响应。我知道我走的是正确的路线,因为它正在响应后端的正确日志。但我无法得到 res.end 工作。我尝试了其他方法以及来自这篇文章的示例。如何从 Node.js 中的 ajax 帖子返回成功。
$.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;
解决方案
它应该是 res.send 你已经使用了 res.end 。请使用正确的语法。res.end() 将结束响应过程。
推荐阅读
- python - PyToch Big Graph 的嵌入数据集中优化器的 state_dict 的目的是什么?
- java - 如何使用 Spring Kafka 处理两个时间戳之间的所有消息?
- excel - Excel 格式化行
- javascript - React jsPdf html2Canvas utf-8 字体问题
- java - 日期格式的 java LocalDate 中的错误
- javascript - 在 Vue 环境(或任何其他 JS 框架)中防止页面访问
- typescript - mqtt:net.createConnection 不是函数
- redis - Redis 在复制中使用哪种提交协议与副本通信?
- sql-server - 在 Azure Data Studio 和 SSMS 或 SSRS 中执行 SQL 查询时的不同行为
- java - 无法弄清楚如何获得正确的输出