简体   繁体   English

从客户端JS,服务器Nodejs发送JSON作为[对象对象]读取

[英]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.

相关问题 将JSON对象从客户端传递到服务器 - Passing JSON object from client to server 如何将JSON中的ArrayBuffer从JS客户端发送到Node.js服务器? - How to send ArrayBuffer in json from js client to nodejs server? 在Node.js / Express.js中,如何将JSON对象从服务器传输到客户端? - In Node.js/Express.js, how can I transfer a JSON object from server to client? 从NodeJS服务器向Web客户端发送消息 - Sending messages from NodeJS server to Web client 将对象从服务器发送到客户端(node / express / ejs) - Sending an object from the server to the client (node/express/ejs) 将对象从节点服务器发送到客户端的 javascript - Sending object from node server to javascript on client side 如何在Handlebars(客户端)中打印从Node JS服务器发送的JSON对象数组? - How to print a JSON object array in Handlebars (client side), that is sent from Node JS server? 如何从服务器(Node JS和Express)的客户端上接收和使用JSON对象? - How do I receive and use a JSON object on the client-side from the server (Node JS and Express)? 通过JSON从JSON发送服务器端对象到jqGrid - Sending a server-side object via JSON from Javascript for jqGrid Node.js服务器将另一个对象发送给客户端 - Nodejs server send another object to client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM