Javascript req.body.item未定义-删除请求express js
我是web开发的初学者,在向本地RESTAPI(使用Express Js编写)发送删除请求时,检索参数时遇到困难。我已经在谷歌上搜索了这个问题,但大多数问题都是通过使用主体解析器解决的 当我返回并将req.body打印回控制台时,它显示为:Javascript req.body.item未定义-删除请求express js,javascript,jquery,node.js,ajax,rest,Javascript,Jquery,Node.js,Ajax,Rest,我是web开发的初学者,在向本地RESTAPI(使用Express Js编写)发送删除请求时,检索参数时遇到困难。我已经在谷歌上搜索了这个问题,但大多数问题都是通过使用主体解析器解决的 当我返回并将req.body打印回控制台时,它显示为: {data: "{"Customer":"1"}"} 哪个看起来正确? 但是当我试着回忆的时候 req.body.Customer; 在routes.js文件中,它显示为未定义 我是不是错过了一些很明显的东西 发出请求的JQuery函数 function
{data: "{"Customer":"1"}"}
哪个看起来正确?
但是当我试着回忆的时候
req.body.Customer;
在routes.js文件中,它显示为未定义
我是不是错过了一些很明显的东西
发出请求的JQuery函数
function DeleteItem(){
let data = {
Customer: $customerId.text()
}
data = JSON.stringify(data);
$.ajax({
url: "http://localhost:3000/Customers",
type: 'DELETE',
data: {
data
},
success: function(res) {
console.log(res);
BuildTable();
},
error: function(res) {
console.log(res);
alert(res);
}
});
}
Routes.js
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var appRouter = function(app) {
app.delete("/Customers", function(req, res) {
var customer = req.body.Customer;
console.log(customer);
res.send(req.body);
});
}
module.exports = appRouter;
首先删除行
data=JSON.stringify(data)代码>
然后,如果您需要客户,您应该写:
req.body.data.Customer;
或者您可以这样更改您的请求:
function DeleteItem(){
let data = {
Customer: $customerId.text()
}
$.ajax({
url: "http://localhost:3000/Customers",
type: 'DELETE',
data: data,
success: function(res) {
console.log(res);
BuildTable();
},
error: function(res) {
console.log(res);
alert(res);
}
});
}
现在您正在创建一个新的对象数据,并将之前创建的对象分配给它。首先删除行data=JSON.stringify(数据)代码>
然后,如果您需要客户,您应该写:
req.body.data.Customer;
或者您可以这样更改您的请求:
function DeleteItem(){
let data = {
Customer: $customerId.text()
}
$.ajax({
url: "http://localhost:3000/Customers",
type: 'DELETE',
data: data,
success: function(res) {
console.log(res);
BuildTable();
},
error: function(res) {
console.log(res);
alert(res);
}
});
}
现在,您正在创建一个新的对象数据,并将之前创建的对象指定给它