简体   繁体   中英

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". 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.

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.

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 .

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:

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"

If you are using expres 4.16 or higher use

app.use(express.json());

There is no need to use bodyParser

Try

console.log('We got a Post request %O', req.body);

Reference: 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

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.

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

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 will help you in extracting req.body content, follow below link for how to use in Node JS. https://blog.fullstacktraining.com/how-do-you-extract-post-data-in-node-js/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM