I am making an AJAX POST request with multiple objects to a node.js server. Although my server sends status code 200
, I am still getting the error Javascript AJAX SyntaxError: Unexpected token E in JSON at position 0
. Here is my POST request:
var pany_id = "some_generic_id";
var president = "obama";
var postData = {
pany_id : pany_id,
president : president
};
$.ajax({
type: "POST",
url: '/api/test_link',
data: JSON.stringify(postData),
contentType: "application/json; charset=utf-8",
dataType: "json",
data: postData,
success: function(data, status) {
console.log('it worked!')
},
error: function(request, status, error) {
console.log(request);
console.log(status);
console.log(error);
}
});
And here is my server side code:
app.post('/api/test_link', function(req, res) {
console.log('--post data--');
console.log(req.body);
/*
prints out:
--post data--
{ pany_id: 'pany_id', president: 'obama' }
*/
res.sendStatus(200);
});
Here's an image from my network tab:
Does anyone know what I might be missing or why my postData
has invalid syntax?
I am making an AJAX POST request with multiple objects to a node.js server. Although my server sends status code 200
, I am still getting the error Javascript AJAX SyntaxError: Unexpected token E in JSON at position 0
. Here is my POST request:
var pany_id = "some_generic_id";
var president = "obama";
var postData = {
pany_id : pany_id,
president : president
};
$.ajax({
type: "POST",
url: '/api/test_link',
data: JSON.stringify(postData),
contentType: "application/json; charset=utf-8",
dataType: "json",
data: postData,
success: function(data, status) {
console.log('it worked!')
},
error: function(request, status, error) {
console.log(request);
console.log(status);
console.log(error);
}
});
And here is my server side code:
app.post('/api/test_link', function(req, res) {
console.log('--post data--');
console.log(req.body);
/*
prints out:
--post data--
{ pany_id: 'pany_id', president: 'obama' }
*/
res.sendStatus(200);
});
Here's an image from my network tab:
Does anyone know what I might be missing or why my postData
has invalid syntax?
- check your network response see it's result? – Beginner Commented Dec 27, 2016 at 6:09
- my network response is a status 200.. but since it returned 200, doesn't it mean the AJAX request was successful (instead of error)? – Trung Tran Commented Dec 27, 2016 at 6:11
- does it displayed the json object in your network response? check that – Beginner Commented Dec 27, 2016 at 6:13
- I see it's display but not in Content-Type: application/json format see my answer below – Beginner Commented Dec 27, 2016 at 6:16
- 1 @Maximus I see, I didn't realise that I thought he want's to return something/he's returning something on it since in his post he put a json object sample, problem is not enough detail. – Beginner Commented Dec 27, 2016 at 6:38
3 Answers
Reset to default 5The docs on ajax call states about dataType
option:
The type of data that you're expecting back from the server. "json": Evaluates the response as JSON and returns a JavaScript object.
Since you're not returning any data from the server, your empty data is parsed as JSON, which produces the error. Simply remove dataType: "json"
if you're not returning any data.
add res.writeHead(200, {"Content-Type": "application/json"});
at the beginning of app.post('/api/test_link', function(req, res) {
to specify that you wanted response as json format
Remove your
res.sendStatus(200);
Since res.writeHead(200, {'Content-Type': 'application/json'});
will also set your statusCode
So it would be like this
app.post('/api/test_link', function(req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
console.log('--post data--');
console.log(req.body);
/*
prints out:
--post data--
{ pany_id: 'pany_id', president: 'obama' }
*/
res.send();
});
I face this error if I'm not using JSON.stringify()
:
$.post(url, body, res => { ... });
// Error: {message: "Unexpected token e in JSON at position 0"}
$.post(url, JSON.stringify(body), res => { ... });