posting json to express - invalid json
express, json, node.js
Solution
This code may help you :
var jsondataResource = JSON.stringify({id: '435ghgf545ft5345', allowed: true});
$.ajax({
type: 'POST', //GET or POST or PUT or DELETE verb
async: false,
url: '/admin', // Location of the service
data: jsondataResource , //Data sent to server
contentType: 'application/json', // content type sent to server
dataType: 'json', //Expected data format from server
processdata: true, //True or False
crossDomain: true,
success: function (msg, textStatus, xmlHttp) {
result = msg;
},
error: ServiceFailed // When Service call fails
});
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;
}
Problem
I am trying to post some json to a node server running express but it keeps telling me the json was invalid. But its not, its just a plain old object. Currently I get the error 'unexpected token i' client: ``` $.ajax({ contentType: 'application/json', type: "POST", url: "/admin", data: {id: '435ghgf545ft5345', allowed: true} }); ``` server: ``` var bodyParser = require('body-parser'); app.use(bodyParser({strict: false})); app.post('/admin', function(request, response) { console.log(request.body); }); ``` I have also tried putting bodyParser.json() as the second parameter in the post route and get the error 'invalid json at parse'. I cant figure out why.