最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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?

Share Improve this question edited Dec 27, 2016 at 6:16 Trung Tran asked Dec 27, 2016 at 6:05 Trung TranTrung Tran 13.8k48 gold badges126 silver badges209 bronze badges 14
  • 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
 |  Show 9 more ments

3 Answers 3

Reset to default 5

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.

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 => { ... });  
发布评论

评论列表(0)

  1. 暂无评论