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 5,777
1 REPLY 1

Not applicable

Hi @childsafetyapp . The slashes are not in the string. Slashes are used to represent characters such as " because " is used as a starting/ending delimiter for strings.

If you were to console.log the session of that object you printed, it would be printed without the slashes.

So, if you want to prevent backslashes to be removed, try replacing double quotes with single quotes.