【发布时间】:2023-02-25 10:17:28
【问题描述】:
我正在使用 AJAX 发布 JSON:
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}))
app.use(express.urlencoded({ extended: true}));
const rowObject=JSON.stringify(rowData)
$.ajax({
type: "POST",
url: '/api/entities/liquid',
data: rowObject,
dataType: 'application/json',
success: function(){
//success code here
},
error: function(){
//error code here
}
});
return rowObject
当我使用 console.log(req.body) 时,它返回以下内容:
{
'{"key1":"value1","key2":"value2"...}': ''
}
我应该能够使用 req.body.value1 提取 value1 但它始终未定义。客户端在数组 [0] 中显示 [{"key1":"value1","key2":"value2"}]: 的有效负载我收到 400 响应,但我知道那是因为我无法提取值。我试过 req.query、req.params,还尝试添加数组的索引,如 req.body[0].key1 - 仍然未定义。我是新手,所以这可能只是一个基本的 js 问题,但我不明白为什么提取值如此复杂。我有另一种使用 data[0].value1 格式可以正常工作的表单。我也尝试使用“数据”,但无法识别。
【问题讨论】:
标签: javascript node.js json ajax req