[英]Sending JSON from Client JS, Server Nodejs Reads as [object object]
Hello I'm simply trying to have a welcome message that gets updated via POST, and as far as I can tell I'm sending a JSON from my Client side JavaScript, and on my NodeJS side it shows as [object object] I've tried req.body and that comes back with "undefined". 您好,我只是想尝试通过POST更新欢迎消息,据我所知,我是从客户端JavaScript发送JSON,而在NodeJS方面,它显示为[object object] ve尝试了req.body,然后返回“ undefined”。 I'm wondering how I would be able to extract my welcome message from the JSON i'm sending to the nodejs server and save to a .JSON to be able to be pulled later from the client. 我想知道如何从发送到Node.js服务器的JSON中提取欢迎消息,并保存到.JSON,以便以后从客户端提取。
I've tried doing jsonstringify(req)
and that returns a big error in my nodejs cmd which I can paste if that may be necessary. 我尝试做jsonstringify(req)
,这会在我的nodejs cmd中返回一个大错误,如果有必要,我可以粘贴该错误。
nodejs server POST, and it will write to the file welcome.json, it will either write [object object]
or undefined
, based on if I use req.body
or req
. nodejs服务器POST,它将写入文件welcome.json,根据我使用req.body
还是req
,它将写入[object object]
或undefined
。
app.post('/update', function (req, res) {
fs.writeFile(__dirname + '/rqsts/welcome.json', req.body, function () {
console.log('We got a Post request' + req.body);
});
});
and here is my client side http post request: 这是我的客户端http发布请求:
function submit() {
var text_Input = document.getElementById('textinput').value;
var testing = document.getElementById('testme');
var welcome_array = {
welcome: ""
};
welcome_array.welcome = text_Input;
var welcomeJSON = JSON.stringify(welcome_array);
var url = 'http://localhost:8080/update';
var http = new XMLHttpRequest();
http.open('POST', url, false); // false for synchronous request
Add_Para(welcomeJSON, testing);
http.send(welcomeJSON);
}
the Add_Para is a function I made to troubleshoot, it adds a paragraph to said html with the requested data "welcomeJSON" Add_Para是我进行故障排除的功能,它向带有要求的数据“ welcomeJSON”的html添加了一个段落
If you are using expres 4.16 or higher use 如果您使用的是expres 4.16或更高版本
app.use(express.json());
There is no need to use bodyParser
无需使用bodyParser
Try 尝试
console.log('We got a Post request %O', req.body);
Reference: https://developer.mozilla.org/en-US/docs/Web/API/Console 参考: https : //developer.mozilla.org/en-US/docs/Web/API/Console
Example: 例:
const a = {
a: 'a',
};
console.log('We got a Post request %O', a); // We got a Post request { a: 'a' }
Or you may try using JSON.stringify 或者您可以尝试使用JSON.stringify
Example: 例:
const a = {
a: 'a',
};
console.log('We got a Post request ' + JSON.stringify(a)); // We got a Post request {"a":"a"}
Hello I have found my solution, I believe I was missing including body-parser was the main issue, here is my now update code. 您好,我找到了解决方案,我相信主要的问题是缺少body-parser,这是我现在的更新代码。
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.post('/update', function (req, res) {
console.log('We got a Post request "%O"', req.body.welcome);
var now = new Date();
console.log('Time of change: ' + now.getHours() + ":" + now.getMinutes() + " " + now.getDate() + "/" + now.getMonth() + "/" + now.getFullYear());
fs.writeFile(__dirname + '/rqsts/welcome.json', JSON.stringify(req.body), function () {
});
});
client side js 客户端js
function submit() {
var text_Input = document.getElementById('textinput').value;
var testing = document.getElementById('testme');
var welcome_array = {
"welcome": ""
};
welcome_array.welcome = text_Input;
var welcomeJSON = JSON.stringify(welcome_array);
var url = 'http://localhost:8080/update';
var http = new XMLHttpRequest();
http.open('POST', url, false);
http.setRequestHeader('Content-type', 'application/json')
Add_Para(welcomeJSON, testing);
http.send(welcomeJSON);
}
Are you using 'body-parser' into express to read req.body? 您是否正在使用“ body-parser”来表达内容以读取req.body?
Body-parser will help you in extracting req.body content, follow below link for how to use in Node JS. Body-parser将帮助您提取req.body内容,请点击以下链接了解如何在Node JS中使用。 https://blog.fullstacktraining.com/how-do-you-extract-post-data-in-node-js/ https://blog.fullstacktraining.com/how-do-you-extract-post-data-in-node-js/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.