Trung Tran
Trung Tran

Reputation: 13771

Javascript AJAX SyntaxError: Unexpected token E in JSON at position 0 in ajax + node.js

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 company_id = "some_generic_id";
     var president  = "obama";

     var postData = {
       company_id   : company_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--
                { company_id: 'company_id', president: 'obama' }

            */  
            res.sendStatus(200);

    });

Here's an image from my network tab:

Network tab

Does anyone know what I might be missing or why my postData has invalid syntax?

Upvotes: 2

Views: 7136

Answers (3)

Dmitry Grinko
Dmitry Grinko

Reputation: 15232

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 => { ... });  

Upvotes: 0

Max Koretskyi
Max Koretskyi

Reputation: 105547

The 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.

Upvotes: 5

Beginner
Beginner

Reputation: 4153

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--
                { company_id: 'company_id', president: 'obama' }

            */  
            res.send();

    });

Upvotes: 1

Related Questions