由于某些原因,当我使用jQuery进行ajax发布时,节点接收到的正文是空的。这是我的ajax帖子:
jQuery
var formData = {
'order': order,
'words': 'words'
};
$.ajax({
type: 'post',
url: 'https://example.com/charge',
processData: false,
data: JSON.stringify(formData),
contentType: 'json',
xhrFields: {
withCredentials: false
},
headers: {
},
success: function (data) {
console.log('Success');
console.log(data);
},
error: function () {
console.log('We are sorry but our servers are having an issue right now');
}
})
这是我的节点代码:
Node
app.js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', routes);
routes/index.js
router.post('/charge', function(req, res) {
console.log(req.body);
} //This always logs {}
我不知道我在这里可能做错了什么。我的浏览器甚至显示了一个有效负载和post请求( formData
对象),但是节点没有记录任何信息。有什么想法吗?
发布于 2014-11-12 04:09:16
使用ajax请求如下:
$.ajax({
type: 'post',
url: 'https://example.com/charge',
data: formData,
xhrFields: {
withCredentials: false
},
headers: {
},
success: function (data) {
console.log('Success');
console.log(data);
},
error: function () {
console.log('We are sorry but our servers are having an issue right now');
}
})
发布于 2020-07-01 08:27:14
我的不起作用,但通过使用,
contentType: 'application/json',
data: JSON.stringify(formdata),
发布于 2014-11-12 04:05:45
检查以下api
通过将processData选项设置为false,可以防止数据自动转换为字符串。
如果要使用json类型,则必须将processData设置为true。
Jquery processData
https://stackoverflow.com/questions/26878975
复制相似问题