[英]jQuery Ajax Post request failing to send data to nodejs server
I'm creating a video streaming site that keeps the backend informed about the video's play status.我正在创建一个视频流媒体网站,让后端了解视频的播放状态。 However when I try to send the data using an ajax request it it showing up as an empty object {}
on the backend:但是,当我尝试使用 ajax 请求发送数据时,它在后端显示为空 object {}
:
Client code客户端代码
data = JSON.stringify({"fish":"pope"})
datasize = data.length
$.ajax({
dataType:"json",
type: "POST",
url:'http://localhost:8080/',
body: data,
success: function(data) {console.log('foobar');}
});
Serverside:服务器端:
app.post("/", (req, res) => {
res.sendStatus(200);
console.log(req.body);
});
I've tried putting the data in the data
reference also which gives undefined
as the output on the server, and setting content-length which is dissallowed.我已经尝试将数据放入data
参考中,这也给出了服务器上的undefined
为 output,并设置了不允许的内容长度。 I've also set Access-Control-Allow-* options, and it's working fine when sending requests with Postman.我还设置了 Access-Control-Allow-* 选项,并且在使用 Postman 发送请求时工作正常。
EDIT: Debug output from browser编辑:从浏览器调试 output
On the advise of a commenter, I used the code generated by POSTMan, which seems to work well.在评论者的建议下,我使用了 POSTMan 生成的代码,似乎效果很好。 I give it below.我在下面给出。
var settings = {
"url": "http://localhost:8080/",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({"foo":"bar"}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.