Javascript 从客户端JS发送JSON,服务器节点JS读取为[object]
您好,我只是想得到一条通过POST更新的欢迎消息,据我所知,我正在从客户端JavaScript发送一个JSON,在我的NodeJS端,它显示为[object object],我尝试了req.body,结果返回“undefined”。我想知道如何从发送到nodejs服务器的JSON中提取欢迎消息,并保存到.JSON中,以便稍后从客户端提取 我尝试过执行Javascript 从客户端JS发送JSON,服务器节点JS读取为[object],javascript,node.js,post,Javascript,Node.js,Post,您好,我只是想得到一条通过POST更新的欢迎消息,据我所知,我正在从客户端JavaScript发送一个JSON,在我的NodeJS端,它显示为[object object],我尝试了req.body,结果返回“undefined”。我想知道如何从发送到nodejs服务器的JSON中提取欢迎消息,并保存到.JSON中,以便稍后从客户端提取 我尝试过执行jsonstringify(req),这会在我的nodejs cmd中返回一个大错误,如果有必要,我可以粘贴它 nodejs服务器POST,它将写入
jsonstringify(req)
,这会在我的nodejs cmd中返回一个大错误,如果有必要,我可以粘贴它
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);
});
});
下面是我的客户端http post请求:
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);
}
Add_Para是我用来解决问题的函数,它用请求的数据“welcomeJSON”将一个段落添加到html中
console.log('We got a Post request %O', req.body);
参考:
例如:
const a = {
a: 'a',
};
console.log('We got a Post request %O', a); // We got a Post request { a: 'a' }
const a = {
a: 'a',
};
console.log('We got a Post request ' + JSON.stringify(a)); // We got a Post request {"a":"a"}
或者您可以尝试使用JSON.stringify
例如:
const a = {
a: 'a',
};
console.log('We got a Post request %O', a); // We got a Post request { a: 'a' }
const a = {
a: 'a',
};
console.log('We got a Post request ' + JSON.stringify(a)); // We got a Post request {"a":"a"}
您好,我已经找到了我的解决方案,我相信我丢失了包括主体解析器在内的主要问题,这里是我现在的更新代码
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 () {
});
});
客户端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);
}
如果您使用的是expres 4.16或更高版本,请使用
app.use(express.json());
无需使用
bodyParser
您是否在express中使用“body parser”来读取req.body
正文解析器将帮助您提取req.Body内容,下面的链接介绍如何在NodeJS中使用。
我们收到了一个Post请求undefinedIt,它正确地记录了变量,我似乎无法将此对象转换为JSON,即使它在发送之前已转换为JSON。您可以先尝试打印req以查看其中的内容。至少它不会记录[object object]来帮助您解决问题。您是想在Nodejs端创建json数组并将其附加到传入的请求中吗?当我尝试req时,它的[object object]req.body返回未定义。当我尝试json.stringify(req)时,我会收到此消息TypeError:根据我的依赖关系,在4.14上将循环结构转换为JSONI am。我可以将package.json更新为4.16吗?没错,我更新了我的版本,并使源代码适合这个版本。谢谢。不客气。如果您觉得答案有效,请接受。@DevinB