VM2022:2 Uncaught SyntaxError: Unexpected token : on AJAX call










1















I send a GET request from the back end to get the json response. I got this error.




Uncaught SyntaxError: Unexpected token :

at DOMEval (jquery-3.3.1.js:111)

at Function.globalEval (jquery-3.3.1.js:345)

at text script (jquery-3.3.1.js:9640)

at ajaxConvert (jquery-3.3.1.js:8787)

at done (jquery-3.3.1.js:9255)

at XMLHttpRequest. (jquery-3.3.1.js:9548)




enter image description here



My AJAX request code is:



$.ajax(
type: "GET", //rest Type
dataType: 'jsonp', //mispelled
url: " url_for('live') ",
contentType: "application/json",
success: function (msg)
console.log(msg);
for (var i = 0; i < msg.counters.length; i++)
var counter = msg.counters[i];
console.log(counter);

,
error: ErrorMsg
);


I have no idea where I went wrong. please help.










share|improve this question
























  • msg is already an object so decoding it again will cause the error you see. You just need to remove the var msg = JSON.decode(msg); line

    – Rory McCrossan
    Nov 13 '18 at 10:59











  • @RoryMcCrossan still getting the error

    – Balakrishnan Sathiyakugan
    Nov 13 '18 at 11:05











  • Is it the same error?

    – Rory McCrossan
    Nov 13 '18 at 11:05











  • @RoryMcCrossan yes same error. VM2335:2 Uncaught SyntaxError: Unexpected token :

    – Balakrishnan Sathiyakugan
    Nov 13 '18 at 11:10






  • 1





    Glad you got it working. I added an answer for you below.

    – Rory McCrossan
    Nov 13 '18 at 11:17















1















I send a GET request from the back end to get the json response. I got this error.




Uncaught SyntaxError: Unexpected token :

at DOMEval (jquery-3.3.1.js:111)

at Function.globalEval (jquery-3.3.1.js:345)

at text script (jquery-3.3.1.js:9640)

at ajaxConvert (jquery-3.3.1.js:8787)

at done (jquery-3.3.1.js:9255)

at XMLHttpRequest. (jquery-3.3.1.js:9548)




enter image description here



My AJAX request code is:



$.ajax(
type: "GET", //rest Type
dataType: 'jsonp', //mispelled
url: " url_for('live') ",
contentType: "application/json",
success: function (msg)
console.log(msg);
for (var i = 0; i < msg.counters.length; i++)
var counter = msg.counters[i];
console.log(counter);

,
error: ErrorMsg
);


I have no idea where I went wrong. please help.










share|improve this question
























  • msg is already an object so decoding it again will cause the error you see. You just need to remove the var msg = JSON.decode(msg); line

    – Rory McCrossan
    Nov 13 '18 at 10:59











  • @RoryMcCrossan still getting the error

    – Balakrishnan Sathiyakugan
    Nov 13 '18 at 11:05











  • Is it the same error?

    – Rory McCrossan
    Nov 13 '18 at 11:05











  • @RoryMcCrossan yes same error. VM2335:2 Uncaught SyntaxError: Unexpected token :

    – Balakrishnan Sathiyakugan
    Nov 13 '18 at 11:10






  • 1





    Glad you got it working. I added an answer for you below.

    – Rory McCrossan
    Nov 13 '18 at 11:17













1












1








1


1






I send a GET request from the back end to get the json response. I got this error.




Uncaught SyntaxError: Unexpected token :

at DOMEval (jquery-3.3.1.js:111)

at Function.globalEval (jquery-3.3.1.js:345)

at text script (jquery-3.3.1.js:9640)

at ajaxConvert (jquery-3.3.1.js:8787)

at done (jquery-3.3.1.js:9255)

at XMLHttpRequest. (jquery-3.3.1.js:9548)




enter image description here



My AJAX request code is:



$.ajax(
type: "GET", //rest Type
dataType: 'jsonp', //mispelled
url: " url_for('live') ",
contentType: "application/json",
success: function (msg)
console.log(msg);
for (var i = 0; i < msg.counters.length; i++)
var counter = msg.counters[i];
console.log(counter);

,
error: ErrorMsg
);


I have no idea where I went wrong. please help.










share|improve this question
















I send a GET request from the back end to get the json response. I got this error.




Uncaught SyntaxError: Unexpected token :

at DOMEval (jquery-3.3.1.js:111)

at Function.globalEval (jquery-3.3.1.js:345)

at text script (jquery-3.3.1.js:9640)

at ajaxConvert (jquery-3.3.1.js:8787)

at done (jquery-3.3.1.js:9255)

at XMLHttpRequest. (jquery-3.3.1.js:9548)




enter image description here



My AJAX request code is:



$.ajax(
type: "GET", //rest Type
dataType: 'jsonp', //mispelled
url: " url_for('live') ",
contentType: "application/json",
success: function (msg)
console.log(msg);
for (var i = 0; i < msg.counters.length; i++)
var counter = msg.counters[i];
console.log(counter);

,
error: ErrorMsg
);


I have no idea where I went wrong. please help.







javascript python jquery json ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 11:05







Balakrishnan Sathiyakugan

















asked Nov 13 '18 at 10:58









Balakrishnan SathiyakuganBalakrishnan Sathiyakugan

587




587












  • msg is already an object so decoding it again will cause the error you see. You just need to remove the var msg = JSON.decode(msg); line

    – Rory McCrossan
    Nov 13 '18 at 10:59











  • @RoryMcCrossan still getting the error

    – Balakrishnan Sathiyakugan
    Nov 13 '18 at 11:05











  • Is it the same error?

    – Rory McCrossan
    Nov 13 '18 at 11:05











  • @RoryMcCrossan yes same error. VM2335:2 Uncaught SyntaxError: Unexpected token :

    – Balakrishnan Sathiyakugan
    Nov 13 '18 at 11:10






  • 1





    Glad you got it working. I added an answer for you below.

    – Rory McCrossan
    Nov 13 '18 at 11:17

















  • msg is already an object so decoding it again will cause the error you see. You just need to remove the var msg = JSON.decode(msg); line

    – Rory McCrossan
    Nov 13 '18 at 10:59











  • @RoryMcCrossan still getting the error

    – Balakrishnan Sathiyakugan
    Nov 13 '18 at 11:05











  • Is it the same error?

    – Rory McCrossan
    Nov 13 '18 at 11:05











  • @RoryMcCrossan yes same error. VM2335:2 Uncaught SyntaxError: Unexpected token :

    – Balakrishnan Sathiyakugan
    Nov 13 '18 at 11:10






  • 1





    Glad you got it working. I added an answer for you below.

    – Rory McCrossan
    Nov 13 '18 at 11:17
















msg is already an object so decoding it again will cause the error you see. You just need to remove the var msg = JSON.decode(msg); line

– Rory McCrossan
Nov 13 '18 at 10:59





msg is already an object so decoding it again will cause the error you see. You just need to remove the var msg = JSON.decode(msg); line

– Rory McCrossan
Nov 13 '18 at 10:59













@RoryMcCrossan still getting the error

– Balakrishnan Sathiyakugan
Nov 13 '18 at 11:05





@RoryMcCrossan still getting the error

– Balakrishnan Sathiyakugan
Nov 13 '18 at 11:05













Is it the same error?

– Rory McCrossan
Nov 13 '18 at 11:05





Is it the same error?

– Rory McCrossan
Nov 13 '18 at 11:05













@RoryMcCrossan yes same error. VM2335:2 Uncaught SyntaxError: Unexpected token :

– Balakrishnan Sathiyakugan
Nov 13 '18 at 11:10





@RoryMcCrossan yes same error. VM2335:2 Uncaught SyntaxError: Unexpected token :

– Balakrishnan Sathiyakugan
Nov 13 '18 at 11:10




1




1





Glad you got it working. I added an answer for you below.

– Rory McCrossan
Nov 13 '18 at 11:17





Glad you got it working. I added an answer for you below.

– Rory McCrossan
Nov 13 '18 at 11:17












1 Answer
1






active

oldest

votes


















1














You have two issues. Firstly the response will already be deserialised for you buy jQuery. Deserialising it again will cause the error you see. Secondly, the response format appears to be JSON, not JSONP, so the dataType property needs to be amended as well. Try this:



$.ajax(
type: "GET",
dataType: 'json',
url: " url_for('live') ",
contentType: "application/json",
success: function (msg)
for (var i = 0; i < msg.counters.length; i++)
var counter = msg.counters[i];
console.log(counter);

,
error: ErrorMsg
);





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279510%2fvm20222-uncaught-syntaxerror-unexpected-token-on-ajax-call%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You have two issues. Firstly the response will already be deserialised for you buy jQuery. Deserialising it again will cause the error you see. Secondly, the response format appears to be JSON, not JSONP, so the dataType property needs to be amended as well. Try this:



    $.ajax(
    type: "GET",
    dataType: 'json',
    url: " url_for('live') ",
    contentType: "application/json",
    success: function (msg)
    for (var i = 0; i < msg.counters.length; i++)
    var counter = msg.counters[i];
    console.log(counter);

    ,
    error: ErrorMsg
    );





    share|improve this answer



























      1














      You have two issues. Firstly the response will already be deserialised for you buy jQuery. Deserialising it again will cause the error you see. Secondly, the response format appears to be JSON, not JSONP, so the dataType property needs to be amended as well. Try this:



      $.ajax(
      type: "GET",
      dataType: 'json',
      url: " url_for('live') ",
      contentType: "application/json",
      success: function (msg)
      for (var i = 0; i < msg.counters.length; i++)
      var counter = msg.counters[i];
      console.log(counter);

      ,
      error: ErrorMsg
      );





      share|improve this answer

























        1












        1








        1







        You have two issues. Firstly the response will already be deserialised for you buy jQuery. Deserialising it again will cause the error you see. Secondly, the response format appears to be JSON, not JSONP, so the dataType property needs to be amended as well. Try this:



        $.ajax(
        type: "GET",
        dataType: 'json',
        url: " url_for('live') ",
        contentType: "application/json",
        success: function (msg)
        for (var i = 0; i < msg.counters.length; i++)
        var counter = msg.counters[i];
        console.log(counter);

        ,
        error: ErrorMsg
        );





        share|improve this answer













        You have two issues. Firstly the response will already be deserialised for you buy jQuery. Deserialising it again will cause the error you see. Secondly, the response format appears to be JSON, not JSONP, so the dataType property needs to be amended as well. Try this:



        $.ajax(
        type: "GET",
        dataType: 'json',
        url: " url_for('live') ",
        contentType: "application/json",
        success: function (msg)
        for (var i = 0; i < msg.counters.length; i++)
        var counter = msg.counters[i];
        console.log(counter);

        ,
        error: ErrorMsg
        );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 11:17









        Rory McCrossanRory McCrossan

        242k29207245




        242k29207245



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279510%2fvm20222-uncaught-syntaxerror-unexpected-token-on-ajax-call%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3