jQuery ajax post generates an errror "Unexpected token : [newline]" on JSON response from Node + Express:
SyntaxError {stack: "SyntaxError: Unexpected token :↵ at eval (nativ…st:3000/javascripts/jquery-1.10.1.min.js:6:18626)", message: "Unexpected token :"}
On the server:
app.post('/api/sav', function(req, res, next) {
var result = {"status": "success"};
res.send(JSON.stringify(result)) // this works
//res.type('json'); // this has no effect
//res.type('application/json'); // this has no effect
//res.send(result); // this fails but should work, no?
//res.json(result); // this fails but should work, no?
});
And on the client
$.ajax({
type: "POST",
url: '/api/sav',
data: content,
success: function(data, textStatus, jqXhr) {
console.log(data);
},
error: function(jqXHR, textStatus,errorThrown ) {
console.log(textStatus);
console.log(errorThrown);
},
dataType: 'json'
});
The server response seems identical in each case:
In the past, I've been able to use res.send(object)
just fine without explicitly stringifying. Can't think of any express.js settings that I've forgotten to set here.