Ali Zia Ali Zia - 1 month ago 6
JSON Question

Converting data into JSON

I have a node server. It takes ajax calls and uses the data in this format.


{"action":"login","data":{"uname":"fas4","password":"jaltheband"}}


I am using a local ajax call and converting my username and password in this format but when I console.log req.body, it shows me a strange format. This is how I am converting data in JSON.

var data = '{"action":"login","data":{"uname":"'+$("#username").val()+'","password","'+$("#password").val()+'"}}';


And then I console.log(req.body), this is displayed.

The formats are different. Why is that so?

enter image description here

Answer

After the "password" key, a double quote followed by colon is missing and the comma is misplaced, try this -

var data = '{"action":"login","data":{"uname":"'+$("#username").val()+'","password":"'+$("#password").val()+'"}}';