Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Eliminating backslashes from JSON using Node and Express

Not applicable

Need some help solving this extra backslash issue in my API. I have an API built in Node running on the Apigee public server. The data I send for javascript looks like perfect JSON, but when it shows up in Node it has all these extra backslashes. I have tried setting the ajax.setRequestHeader to different versions but only the “application/x-www-form-urlencoded” allowed the data to reach Node. //ajax.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); //ajax.setRequestHeader('Content-Type', 'application/json') //ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //ajax.setRequestHeader("Accept", "application/json; charset=utf-8"); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1'); Why is this happening and what can I do to fix it? ====== sample code ====== function postNewPartner(createUserObj) { var ajax = new XMLHttpRequest(); var tempJSON = JSON.stringify(createUserObj); ajax.open('POST', ( 'http://myurl/csa-api/postNewUser')); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1'); ajax.send(tempJSON); ajax.onreadystatechange=function(){ // do stuff here } } }; Data that is sent from javascript tempJSON = {“usertype":"admin","username":"bobsmith","name”:”bob”}” Data received in Node.js (Express) req.body JSONString = {"{\"usertype\":\"admin\",\"username\”:\”bobsmith\”,\”name\”:\”bob\”}":""}

0 1 6,109
1 REPLY 1