I keep getting “Uncaught SyntaxError: Unexpected token o”











up vote
293
down vote

favorite
38












I'm trying to learn some html/css/javascript, so I'm writing myself a teaching project.



The idea was to have some vocabulary contained in a json file which would then be loaded into a table. I managed to load the file in and print out one of its values, after which I began writing the code to load the values into the table.



After doing that I started getting an error, so I removed all the code I had written, leaving me with only one line (the same line that had worked before) ... only the error is still there.



The error is as follows:



Uncaught SyntaxError: Unexpected token o
(anonymous function)script.js:10
jQuery.Callbacks.firejquery-1.7.js:1064
jQuery.Callbacks.self.fireWithjquery-1.7.js:1182
donejquery-1.7.js:7454
jQuery.ajaxTransport.send.callback


My javascript code is contained in a separate file and is simply this:



function loadPageIntoDiv(){
document.getElementById("wokabWeeks").style.display = "block";
}

function loadWokab(){
//also tried getJSON which threw the same error
jQuery.get('wokab.json', function(data) {
var glacier = JSON.parse(data);
});
}


And my JSON file just has the following right now:



[
{
"english": "bag",
"kana": "kaban",
"kanji": "K"
},

{
"english": "glasses",
"kana": "megane",
"kanji": "M"
}
]


Now the error is reported in line 11 which is the var glacier = JSON.parse(data); line.



When I remove the json file I get the error: "GET http://.../wokab.json 404 (Not Found)" so I know it's loading it (or at least trying to).










share|improve this question




















  • 5




    $.get can recognize json when it sent, hence. var glacier = data; should suffice.
    – roselan
    Nov 19 '11 at 14:10






  • 44




    Summing up: you're trying to parse it twice.
    – fiatjaf
    May 31 '13 at 16:38










  • Also see stackoverflow.com/a/42907459/632951
    – Pacerier
    Mar 20 '17 at 15:22















up vote
293
down vote

favorite
38












I'm trying to learn some html/css/javascript, so I'm writing myself a teaching project.



The idea was to have some vocabulary contained in a json file which would then be loaded into a table. I managed to load the file in and print out one of its values, after which I began writing the code to load the values into the table.



After doing that I started getting an error, so I removed all the code I had written, leaving me with only one line (the same line that had worked before) ... only the error is still there.



The error is as follows:



Uncaught SyntaxError: Unexpected token o
(anonymous function)script.js:10
jQuery.Callbacks.firejquery-1.7.js:1064
jQuery.Callbacks.self.fireWithjquery-1.7.js:1182
donejquery-1.7.js:7454
jQuery.ajaxTransport.send.callback


My javascript code is contained in a separate file and is simply this:



function loadPageIntoDiv(){
document.getElementById("wokabWeeks").style.display = "block";
}

function loadWokab(){
//also tried getJSON which threw the same error
jQuery.get('wokab.json', function(data) {
var glacier = JSON.parse(data);
});
}


And my JSON file just has the following right now:



[
{
"english": "bag",
"kana": "kaban",
"kanji": "K"
},

{
"english": "glasses",
"kana": "megane",
"kanji": "M"
}
]


Now the error is reported in line 11 which is the var glacier = JSON.parse(data); line.



When I remove the json file I get the error: "GET http://.../wokab.json 404 (Not Found)" so I know it's loading it (or at least trying to).










share|improve this question




















  • 5




    $.get can recognize json when it sent, hence. var glacier = data; should suffice.
    – roselan
    Nov 19 '11 at 14:10






  • 44




    Summing up: you're trying to parse it twice.
    – fiatjaf
    May 31 '13 at 16:38










  • Also see stackoverflow.com/a/42907459/632951
    – Pacerier
    Mar 20 '17 at 15:22













up vote
293
down vote

favorite
38









up vote
293
down vote

favorite
38






38





I'm trying to learn some html/css/javascript, so I'm writing myself a teaching project.



The idea was to have some vocabulary contained in a json file which would then be loaded into a table. I managed to load the file in and print out one of its values, after which I began writing the code to load the values into the table.



After doing that I started getting an error, so I removed all the code I had written, leaving me with only one line (the same line that had worked before) ... only the error is still there.



The error is as follows:



Uncaught SyntaxError: Unexpected token o
(anonymous function)script.js:10
jQuery.Callbacks.firejquery-1.7.js:1064
jQuery.Callbacks.self.fireWithjquery-1.7.js:1182
donejquery-1.7.js:7454
jQuery.ajaxTransport.send.callback


My javascript code is contained in a separate file and is simply this:



function loadPageIntoDiv(){
document.getElementById("wokabWeeks").style.display = "block";
}

function loadWokab(){
//also tried getJSON which threw the same error
jQuery.get('wokab.json', function(data) {
var glacier = JSON.parse(data);
});
}


And my JSON file just has the following right now:



[
{
"english": "bag",
"kana": "kaban",
"kanji": "K"
},

{
"english": "glasses",
"kana": "megane",
"kanji": "M"
}
]


Now the error is reported in line 11 which is the var glacier = JSON.parse(data); line.



When I remove the json file I get the error: "GET http://.../wokab.json 404 (Not Found)" so I know it's loading it (or at least trying to).










share|improve this question















I'm trying to learn some html/css/javascript, so I'm writing myself a teaching project.



The idea was to have some vocabulary contained in a json file which would then be loaded into a table. I managed to load the file in and print out one of its values, after which I began writing the code to load the values into the table.



After doing that I started getting an error, so I removed all the code I had written, leaving me with only one line (the same line that had worked before) ... only the error is still there.



The error is as follows:



Uncaught SyntaxError: Unexpected token o
(anonymous function)script.js:10
jQuery.Callbacks.firejquery-1.7.js:1064
jQuery.Callbacks.self.fireWithjquery-1.7.js:1182
donejquery-1.7.js:7454
jQuery.ajaxTransport.send.callback


My javascript code is contained in a separate file and is simply this:



function loadPageIntoDiv(){
document.getElementById("wokabWeeks").style.display = "block";
}

function loadWokab(){
//also tried getJSON which threw the same error
jQuery.get('wokab.json', function(data) {
var glacier = JSON.parse(data);
});
}


And my JSON file just has the following right now:



[
{
"english": "bag",
"kana": "kaban",
"kanji": "K"
},

{
"english": "glasses",
"kana": "megane",
"kanji": "M"
}
]


Now the error is reported in line 11 which is the var glacier = JSON.parse(data); line.



When I remove the json file I get the error: "GET http://.../wokab.json 404 (Not Found)" so I know it's loading it (or at least trying to).







javascript jquery json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 '11 at 15:14









Lightness Races in Orbit

280k51452768




280k51452768










asked Nov 10 '11 at 15:11









Bjorninn

1,94032035




1,94032035








  • 5




    $.get can recognize json when it sent, hence. var glacier = data; should suffice.
    – roselan
    Nov 19 '11 at 14:10






  • 44




    Summing up: you're trying to parse it twice.
    – fiatjaf
    May 31 '13 at 16:38










  • Also see stackoverflow.com/a/42907459/632951
    – Pacerier
    Mar 20 '17 at 15:22














  • 5




    $.get can recognize json when it sent, hence. var glacier = data; should suffice.
    – roselan
    Nov 19 '11 at 14:10






  • 44




    Summing up: you're trying to parse it twice.
    – fiatjaf
    May 31 '13 at 16:38










  • Also see stackoverflow.com/a/42907459/632951
    – Pacerier
    Mar 20 '17 at 15:22








5




5




$.get can recognize json when it sent, hence. var glacier = data; should suffice.
– roselan
Nov 19 '11 at 14:10




$.get can recognize json when it sent, hence. var glacier = data; should suffice.
– roselan
Nov 19 '11 at 14:10




44




44




Summing up: you're trying to parse it twice.
– fiatjaf
May 31 '13 at 16:38




Summing up: you're trying to parse it twice.
– fiatjaf
May 31 '13 at 16:38












Also see stackoverflow.com/a/42907459/632951
– Pacerier
Mar 20 '17 at 15:22




Also see stackoverflow.com/a/42907459/632951
– Pacerier
Mar 20 '17 at 15:22












7 Answers
7






active

oldest

votes

















up vote
308
down vote



accepted










Looks like jQuery takes a guess about the datatype. It does the JSON parsing even though you're not calling getJSON()-- then when you try to call JSON.parse() on an object, you're getting the error.



Further explanation can be found in Aditya Mittal's answer.






share|improve this answer



















  • 13




    Aha, so data[0].english returns "bag". Looks like I don't have to parse the json file at all.
    – Bjorninn
    Nov 10 '11 at 15:41






  • 1




    that's interesting.. I guess jquery takes a guess at datatype and assumes it's json. I would think that getJson would work then as well, right?
    – ek_ny
    Nov 10 '11 at 15:54






  • 85




    Small note: if you JSON.parse an object the "Unexpected token o" is thrown simply because it tries to parse obj_to_parse.toString(), which is [object Object]. Try to JSON.parse('[object Object]'); ;)
    – Pier Paolo Ramon
    Feb 14 '12 at 11:48








  • 21




    It happened to me too, I think my error was that I tried to parse to JSON something that already was a JSON Object
    – Wak
    Oct 5 '13 at 9:53






  • 1




    jQuery doesn't guess. If you don't override it with dataType (any why would you), it uses the Content-type HTTP header of the response to determine what sort of data it is, and parses it if it is one that jQuery recognises.
    – Quentin
    Oct 20 '14 at 17:52


















up vote
76
down vote













The problem is very simple



jQuery.get('wokab.json', function(data) {
var glacier = JSON.parse(data);
});


You're parsing it twice. get uses the dataType='json', so data is already in json format.
Use $.ajax({ dataType: 'json' ... to specifically set the returned data type!






share|improve this answer






























    up vote
    44
    down vote













    Basically if the response header is text/html you need to parse, and if the response header is application/json it is already parsed for you.



    Parsed data from jquery success handler for text/html response:



    var parsed = JSON.parse(data);


    Parsed data from jquery success handler for application/json response:



    var parsed = data;





    share|improve this answer

















    • 6




      Note to anyone voting this down, the accepted answer above contains exact copy from this answer. Adding link from accepted answer now.
      – Geoffrey Hale
      Aug 5 '16 at 0:40




















    up vote
    9
    down vote













    Another hints for Unexpected token errors.
    There are two major differences between javascript objects and json:




    1. json data must be always quoted with double quotes.

    2. keys must be quoted


    Correct JSON



     {
    "english": "bag",
    "kana": "kaban",
    "kanji": "K"
    }


    Error JSON 1



     {
    'english': 'bag',
    'kana': 'kaban',
    'kanji': 'K'
    }


    Error JSON 2



     {
    english: "bag",
    kana: "kaban",
    kanji: "K"
    }


    Remark



    This is not a direct answer for that question. But it's an answer for Unexpected token errors. So it may be help others who stumple upon that question.






    share|improve this answer




























      up vote
      2
      down vote













      Simply the response is already parsed, you don't need to parse it again. if you parse it again it will give you "unexpected token o" however you have to specify datatype in your request to be of type dataType='json'






      share|improve this answer






























        up vote
        1
        down vote













        I had a similar problem just now and my solution might help. I'm using an iframe to upload and convert an xml file to json and send it back behind the scenes, and Chrome was adding some garbage to the incoming data that only would show up intermittently and cause the "Uncaught SyntaxError: Unexpected token o" error.



        I was accessing the iframe data like this:



        $('#load-file-iframe').contents().text()


        which worked fine on localhost, but when I uploaded it to the server it stopped working only with some files and only when loading the files in a certain order. I don't really know what caused it, but this fixed it. I changed the line above to



        $('#load-file-iframe').contents().find('body').text()


        once I noticed some garbage in the HTML response.



        Long story short check your raw HTML response data and you might turn something up.






        share|improve this answer





















        • OK, thanks. Strangely it sometimes seems to receive an already parsed json object and sometimes not. I haven't had time to continue the project so I don't know if it will randomly do this (depending on browsers and systems or something). Thanks for the pointer I'll keep it in mind.
          – Bjorninn
          Nov 21 '11 at 14:52


















        up vote
        0
        down vote













        Make sure your JSON file does not have any trailing characters before or after. Maybe an unprintable one? You may want to try this way:



        [{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]





        share|improve this answer



















        • 1




          JSON.parse('[{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]'); Works fine. ¿Have you try replacing that line with alert(data) to check if the file is loading correctly?
          – thexebolud
          Nov 19 '11 at 14:09













        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',
        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%2f8081701%2fi-keep-getting-uncaught-syntaxerror-unexpected-token-o%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        308
        down vote



        accepted










        Looks like jQuery takes a guess about the datatype. It does the JSON parsing even though you're not calling getJSON()-- then when you try to call JSON.parse() on an object, you're getting the error.



        Further explanation can be found in Aditya Mittal's answer.






        share|improve this answer



















        • 13




          Aha, so data[0].english returns "bag". Looks like I don't have to parse the json file at all.
          – Bjorninn
          Nov 10 '11 at 15:41






        • 1




          that's interesting.. I guess jquery takes a guess at datatype and assumes it's json. I would think that getJson would work then as well, right?
          – ek_ny
          Nov 10 '11 at 15:54






        • 85




          Small note: if you JSON.parse an object the "Unexpected token o" is thrown simply because it tries to parse obj_to_parse.toString(), which is [object Object]. Try to JSON.parse('[object Object]'); ;)
          – Pier Paolo Ramon
          Feb 14 '12 at 11:48








        • 21




          It happened to me too, I think my error was that I tried to parse to JSON something that already was a JSON Object
          – Wak
          Oct 5 '13 at 9:53






        • 1




          jQuery doesn't guess. If you don't override it with dataType (any why would you), it uses the Content-type HTTP header of the response to determine what sort of data it is, and parses it if it is one that jQuery recognises.
          – Quentin
          Oct 20 '14 at 17:52















        up vote
        308
        down vote



        accepted










        Looks like jQuery takes a guess about the datatype. It does the JSON parsing even though you're not calling getJSON()-- then when you try to call JSON.parse() on an object, you're getting the error.



        Further explanation can be found in Aditya Mittal's answer.






        share|improve this answer



















        • 13




          Aha, so data[0].english returns "bag". Looks like I don't have to parse the json file at all.
          – Bjorninn
          Nov 10 '11 at 15:41






        • 1




          that's interesting.. I guess jquery takes a guess at datatype and assumes it's json. I would think that getJson would work then as well, right?
          – ek_ny
          Nov 10 '11 at 15:54






        • 85




          Small note: if you JSON.parse an object the "Unexpected token o" is thrown simply because it tries to parse obj_to_parse.toString(), which is [object Object]. Try to JSON.parse('[object Object]'); ;)
          – Pier Paolo Ramon
          Feb 14 '12 at 11:48








        • 21




          It happened to me too, I think my error was that I tried to parse to JSON something that already was a JSON Object
          – Wak
          Oct 5 '13 at 9:53






        • 1




          jQuery doesn't guess. If you don't override it with dataType (any why would you), it uses the Content-type HTTP header of the response to determine what sort of data it is, and parses it if it is one that jQuery recognises.
          – Quentin
          Oct 20 '14 at 17:52













        up vote
        308
        down vote



        accepted







        up vote
        308
        down vote



        accepted






        Looks like jQuery takes a guess about the datatype. It does the JSON parsing even though you're not calling getJSON()-- then when you try to call JSON.parse() on an object, you're getting the error.



        Further explanation can be found in Aditya Mittal's answer.






        share|improve this answer














        Looks like jQuery takes a guess about the datatype. It does the JSON parsing even though you're not calling getJSON()-- then when you try to call JSON.parse() on an object, you're getting the error.



        Further explanation can be found in Aditya Mittal's answer.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 12:26









        Community

        11




        11










        answered Nov 10 '11 at 15:14









        ek_ny

        8,16453752




        8,16453752








        • 13




          Aha, so data[0].english returns "bag". Looks like I don't have to parse the json file at all.
          – Bjorninn
          Nov 10 '11 at 15:41






        • 1




          that's interesting.. I guess jquery takes a guess at datatype and assumes it's json. I would think that getJson would work then as well, right?
          – ek_ny
          Nov 10 '11 at 15:54






        • 85




          Small note: if you JSON.parse an object the "Unexpected token o" is thrown simply because it tries to parse obj_to_parse.toString(), which is [object Object]. Try to JSON.parse('[object Object]'); ;)
          – Pier Paolo Ramon
          Feb 14 '12 at 11:48








        • 21




          It happened to me too, I think my error was that I tried to parse to JSON something that already was a JSON Object
          – Wak
          Oct 5 '13 at 9:53






        • 1




          jQuery doesn't guess. If you don't override it with dataType (any why would you), it uses the Content-type HTTP header of the response to determine what sort of data it is, and parses it if it is one that jQuery recognises.
          – Quentin
          Oct 20 '14 at 17:52














        • 13




          Aha, so data[0].english returns "bag". Looks like I don't have to parse the json file at all.
          – Bjorninn
          Nov 10 '11 at 15:41






        • 1




          that's interesting.. I guess jquery takes a guess at datatype and assumes it's json. I would think that getJson would work then as well, right?
          – ek_ny
          Nov 10 '11 at 15:54






        • 85




          Small note: if you JSON.parse an object the "Unexpected token o" is thrown simply because it tries to parse obj_to_parse.toString(), which is [object Object]. Try to JSON.parse('[object Object]'); ;)
          – Pier Paolo Ramon
          Feb 14 '12 at 11:48








        • 21




          It happened to me too, I think my error was that I tried to parse to JSON something that already was a JSON Object
          – Wak
          Oct 5 '13 at 9:53






        • 1




          jQuery doesn't guess. If you don't override it with dataType (any why would you), it uses the Content-type HTTP header of the response to determine what sort of data it is, and parses it if it is one that jQuery recognises.
          – Quentin
          Oct 20 '14 at 17:52








        13




        13




        Aha, so data[0].english returns "bag". Looks like I don't have to parse the json file at all.
        – Bjorninn
        Nov 10 '11 at 15:41




        Aha, so data[0].english returns "bag". Looks like I don't have to parse the json file at all.
        – Bjorninn
        Nov 10 '11 at 15:41




        1




        1




        that's interesting.. I guess jquery takes a guess at datatype and assumes it's json. I would think that getJson would work then as well, right?
        – ek_ny
        Nov 10 '11 at 15:54




        that's interesting.. I guess jquery takes a guess at datatype and assumes it's json. I would think that getJson would work then as well, right?
        – ek_ny
        Nov 10 '11 at 15:54




        85




        85




        Small note: if you JSON.parse an object the "Unexpected token o" is thrown simply because it tries to parse obj_to_parse.toString(), which is [object Object]. Try to JSON.parse('[object Object]'); ;)
        – Pier Paolo Ramon
        Feb 14 '12 at 11:48






        Small note: if you JSON.parse an object the "Unexpected token o" is thrown simply because it tries to parse obj_to_parse.toString(), which is [object Object]. Try to JSON.parse('[object Object]'); ;)
        – Pier Paolo Ramon
        Feb 14 '12 at 11:48






        21




        21




        It happened to me too, I think my error was that I tried to parse to JSON something that already was a JSON Object
        – Wak
        Oct 5 '13 at 9:53




        It happened to me too, I think my error was that I tried to parse to JSON something that already was a JSON Object
        – Wak
        Oct 5 '13 at 9:53




        1




        1




        jQuery doesn't guess. If you don't override it with dataType (any why would you), it uses the Content-type HTTP header of the response to determine what sort of data it is, and parses it if it is one that jQuery recognises.
        – Quentin
        Oct 20 '14 at 17:52




        jQuery doesn't guess. If you don't override it with dataType (any why would you), it uses the Content-type HTTP header of the response to determine what sort of data it is, and parses it if it is one that jQuery recognises.
        – Quentin
        Oct 20 '14 at 17:52












        up vote
        76
        down vote













        The problem is very simple



        jQuery.get('wokab.json', function(data) {
        var glacier = JSON.parse(data);
        });


        You're parsing it twice. get uses the dataType='json', so data is already in json format.
        Use $.ajax({ dataType: 'json' ... to specifically set the returned data type!






        share|improve this answer



























          up vote
          76
          down vote













          The problem is very simple



          jQuery.get('wokab.json', function(data) {
          var glacier = JSON.parse(data);
          });


          You're parsing it twice. get uses the dataType='json', so data is already in json format.
          Use $.ajax({ dataType: 'json' ... to specifically set the returned data type!






          share|improve this answer

























            up vote
            76
            down vote










            up vote
            76
            down vote









            The problem is very simple



            jQuery.get('wokab.json', function(data) {
            var glacier = JSON.parse(data);
            });


            You're parsing it twice. get uses the dataType='json', so data is already in json format.
            Use $.ajax({ dataType: 'json' ... to specifically set the returned data type!






            share|improve this answer














            The problem is very simple



            jQuery.get('wokab.json', function(data) {
            var glacier = JSON.parse(data);
            });


            You're parsing it twice. get uses the dataType='json', so data is already in json format.
            Use $.ajax({ dataType: 'json' ... to specifically set the returned data type!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 '14 at 14:25









            ProblemsOfSumit

            8,57432947




            8,57432947










            answered Feb 6 '12 at 3:19









            Andrius Bentkus

            1,2081023




            1,2081023






















                up vote
                44
                down vote













                Basically if the response header is text/html you need to parse, and if the response header is application/json it is already parsed for you.



                Parsed data from jquery success handler for text/html response:



                var parsed = JSON.parse(data);


                Parsed data from jquery success handler for application/json response:



                var parsed = data;





                share|improve this answer

















                • 6




                  Note to anyone voting this down, the accepted answer above contains exact copy from this answer. Adding link from accepted answer now.
                  – Geoffrey Hale
                  Aug 5 '16 at 0:40

















                up vote
                44
                down vote













                Basically if the response header is text/html you need to parse, and if the response header is application/json it is already parsed for you.



                Parsed data from jquery success handler for text/html response:



                var parsed = JSON.parse(data);


                Parsed data from jquery success handler for application/json response:



                var parsed = data;





                share|improve this answer

















                • 6




                  Note to anyone voting this down, the accepted answer above contains exact copy from this answer. Adding link from accepted answer now.
                  – Geoffrey Hale
                  Aug 5 '16 at 0:40















                up vote
                44
                down vote










                up vote
                44
                down vote









                Basically if the response header is text/html you need to parse, and if the response header is application/json it is already parsed for you.



                Parsed data from jquery success handler for text/html response:



                var parsed = JSON.parse(data);


                Parsed data from jquery success handler for application/json response:



                var parsed = data;





                share|improve this answer












                Basically if the response header is text/html you need to parse, and if the response header is application/json it is already parsed for you.



                Parsed data from jquery success handler for text/html response:



                var parsed = JSON.parse(data);


                Parsed data from jquery success handler for application/json response:



                var parsed = data;






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 3 '15 at 0:55









                Aditya Mittal

                1,0501010




                1,0501010








                • 6




                  Note to anyone voting this down, the accepted answer above contains exact copy from this answer. Adding link from accepted answer now.
                  – Geoffrey Hale
                  Aug 5 '16 at 0:40
















                • 6




                  Note to anyone voting this down, the accepted answer above contains exact copy from this answer. Adding link from accepted answer now.
                  – Geoffrey Hale
                  Aug 5 '16 at 0:40










                6




                6




                Note to anyone voting this down, the accepted answer above contains exact copy from this answer. Adding link from accepted answer now.
                – Geoffrey Hale
                Aug 5 '16 at 0:40






                Note to anyone voting this down, the accepted answer above contains exact copy from this answer. Adding link from accepted answer now.
                – Geoffrey Hale
                Aug 5 '16 at 0:40












                up vote
                9
                down vote













                Another hints for Unexpected token errors.
                There are two major differences between javascript objects and json:




                1. json data must be always quoted with double quotes.

                2. keys must be quoted


                Correct JSON



                 {
                "english": "bag",
                "kana": "kaban",
                "kanji": "K"
                }


                Error JSON 1



                 {
                'english': 'bag',
                'kana': 'kaban',
                'kanji': 'K'
                }


                Error JSON 2



                 {
                english: "bag",
                kana: "kaban",
                kanji: "K"
                }


                Remark



                This is not a direct answer for that question. But it's an answer for Unexpected token errors. So it may be help others who stumple upon that question.






                share|improve this answer

























                  up vote
                  9
                  down vote













                  Another hints for Unexpected token errors.
                  There are two major differences between javascript objects and json:




                  1. json data must be always quoted with double quotes.

                  2. keys must be quoted


                  Correct JSON



                   {
                  "english": "bag",
                  "kana": "kaban",
                  "kanji": "K"
                  }


                  Error JSON 1



                   {
                  'english': 'bag',
                  'kana': 'kaban',
                  'kanji': 'K'
                  }


                  Error JSON 2



                   {
                  english: "bag",
                  kana: "kaban",
                  kanji: "K"
                  }


                  Remark



                  This is not a direct answer for that question. But it's an answer for Unexpected token errors. So it may be help others who stumple upon that question.






                  share|improve this answer























                    up vote
                    9
                    down vote










                    up vote
                    9
                    down vote









                    Another hints for Unexpected token errors.
                    There are two major differences between javascript objects and json:




                    1. json data must be always quoted with double quotes.

                    2. keys must be quoted


                    Correct JSON



                     {
                    "english": "bag",
                    "kana": "kaban",
                    "kanji": "K"
                    }


                    Error JSON 1



                     {
                    'english': 'bag',
                    'kana': 'kaban',
                    'kanji': 'K'
                    }


                    Error JSON 2



                     {
                    english: "bag",
                    kana: "kaban",
                    kanji: "K"
                    }


                    Remark



                    This is not a direct answer for that question. But it's an answer for Unexpected token errors. So it may be help others who stumple upon that question.






                    share|improve this answer












                    Another hints for Unexpected token errors.
                    There are two major differences between javascript objects and json:




                    1. json data must be always quoted with double quotes.

                    2. keys must be quoted


                    Correct JSON



                     {
                    "english": "bag",
                    "kana": "kaban",
                    "kanji": "K"
                    }


                    Error JSON 1



                     {
                    'english': 'bag',
                    'kana': 'kaban',
                    'kanji': 'K'
                    }


                    Error JSON 2



                     {
                    english: "bag",
                    kana: "kaban",
                    kanji: "K"
                    }


                    Remark



                    This is not a direct answer for that question. But it's an answer for Unexpected token errors. So it may be help others who stumple upon that question.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 5 '16 at 10:04









                    Matthias M

                    3,63154258




                    3,63154258






















                        up vote
                        2
                        down vote













                        Simply the response is already parsed, you don't need to parse it again. if you parse it again it will give you "unexpected token o" however you have to specify datatype in your request to be of type dataType='json'






                        share|improve this answer



























                          up vote
                          2
                          down vote













                          Simply the response is already parsed, you don't need to parse it again. if you parse it again it will give you "unexpected token o" however you have to specify datatype in your request to be of type dataType='json'






                          share|improve this answer

























                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            Simply the response is already parsed, you don't need to parse it again. if you parse it again it will give you "unexpected token o" however you have to specify datatype in your request to be of type dataType='json'






                            share|improve this answer














                            Simply the response is already parsed, you don't need to parse it again. if you parse it again it will give you "unexpected token o" however you have to specify datatype in your request to be of type dataType='json'







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 21 '15 at 11:43

























                            answered Sep 29 '14 at 20:56









                            msoliman

                            6,53913834




                            6,53913834






















                                up vote
                                1
                                down vote













                                I had a similar problem just now and my solution might help. I'm using an iframe to upload and convert an xml file to json and send it back behind the scenes, and Chrome was adding some garbage to the incoming data that only would show up intermittently and cause the "Uncaught SyntaxError: Unexpected token o" error.



                                I was accessing the iframe data like this:



                                $('#load-file-iframe').contents().text()


                                which worked fine on localhost, but when I uploaded it to the server it stopped working only with some files and only when loading the files in a certain order. I don't really know what caused it, but this fixed it. I changed the line above to



                                $('#load-file-iframe').contents().find('body').text()


                                once I noticed some garbage in the HTML response.



                                Long story short check your raw HTML response data and you might turn something up.






                                share|improve this answer





















                                • OK, thanks. Strangely it sometimes seems to receive an already parsed json object and sometimes not. I haven't had time to continue the project so I don't know if it will randomly do this (depending on browsers and systems or something). Thanks for the pointer I'll keep it in mind.
                                  – Bjorninn
                                  Nov 21 '11 at 14:52















                                up vote
                                1
                                down vote













                                I had a similar problem just now and my solution might help. I'm using an iframe to upload and convert an xml file to json and send it back behind the scenes, and Chrome was adding some garbage to the incoming data that only would show up intermittently and cause the "Uncaught SyntaxError: Unexpected token o" error.



                                I was accessing the iframe data like this:



                                $('#load-file-iframe').contents().text()


                                which worked fine on localhost, but when I uploaded it to the server it stopped working only with some files and only when loading the files in a certain order. I don't really know what caused it, but this fixed it. I changed the line above to



                                $('#load-file-iframe').contents().find('body').text()


                                once I noticed some garbage in the HTML response.



                                Long story short check your raw HTML response data and you might turn something up.






                                share|improve this answer





















                                • OK, thanks. Strangely it sometimes seems to receive an already parsed json object and sometimes not. I haven't had time to continue the project so I don't know if it will randomly do this (depending on browsers and systems or something). Thanks for the pointer I'll keep it in mind.
                                  – Bjorninn
                                  Nov 21 '11 at 14:52













                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote









                                I had a similar problem just now and my solution might help. I'm using an iframe to upload and convert an xml file to json and send it back behind the scenes, and Chrome was adding some garbage to the incoming data that only would show up intermittently and cause the "Uncaught SyntaxError: Unexpected token o" error.



                                I was accessing the iframe data like this:



                                $('#load-file-iframe').contents().text()


                                which worked fine on localhost, but when I uploaded it to the server it stopped working only with some files and only when loading the files in a certain order. I don't really know what caused it, but this fixed it. I changed the line above to



                                $('#load-file-iframe').contents().find('body').text()


                                once I noticed some garbage in the HTML response.



                                Long story short check your raw HTML response data and you might turn something up.






                                share|improve this answer












                                I had a similar problem just now and my solution might help. I'm using an iframe to upload and convert an xml file to json and send it back behind the scenes, and Chrome was adding some garbage to the incoming data that only would show up intermittently and cause the "Uncaught SyntaxError: Unexpected token o" error.



                                I was accessing the iframe data like this:



                                $('#load-file-iframe').contents().text()


                                which worked fine on localhost, but when I uploaded it to the server it stopped working only with some files and only when loading the files in a certain order. I don't really know what caused it, but this fixed it. I changed the line above to



                                $('#load-file-iframe').contents().find('body').text()


                                once I noticed some garbage in the HTML response.



                                Long story short check your raw HTML response data and you might turn something up.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 19 '11 at 13:53









                                Brandon

                                1,6751617




                                1,6751617












                                • OK, thanks. Strangely it sometimes seems to receive an already parsed json object and sometimes not. I haven't had time to continue the project so I don't know if it will randomly do this (depending on browsers and systems or something). Thanks for the pointer I'll keep it in mind.
                                  – Bjorninn
                                  Nov 21 '11 at 14:52


















                                • OK, thanks. Strangely it sometimes seems to receive an already parsed json object and sometimes not. I haven't had time to continue the project so I don't know if it will randomly do this (depending on browsers and systems or something). Thanks for the pointer I'll keep it in mind.
                                  – Bjorninn
                                  Nov 21 '11 at 14:52
















                                OK, thanks. Strangely it sometimes seems to receive an already parsed json object and sometimes not. I haven't had time to continue the project so I don't know if it will randomly do this (depending on browsers and systems or something). Thanks for the pointer I'll keep it in mind.
                                – Bjorninn
                                Nov 21 '11 at 14:52




                                OK, thanks. Strangely it sometimes seems to receive an already parsed json object and sometimes not. I haven't had time to continue the project so I don't know if it will randomly do this (depending on browsers and systems or something). Thanks for the pointer I'll keep it in mind.
                                – Bjorninn
                                Nov 21 '11 at 14:52










                                up vote
                                0
                                down vote













                                Make sure your JSON file does not have any trailing characters before or after. Maybe an unprintable one? You may want to try this way:



                                [{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]





                                share|improve this answer



















                                • 1




                                  JSON.parse('[{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]'); Works fine. ¿Have you try replacing that line with alert(data) to check if the file is loading correctly?
                                  – thexebolud
                                  Nov 19 '11 at 14:09

















                                up vote
                                0
                                down vote













                                Make sure your JSON file does not have any trailing characters before or after. Maybe an unprintable one? You may want to try this way:



                                [{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]





                                share|improve this answer



















                                • 1




                                  JSON.parse('[{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]'); Works fine. ¿Have you try replacing that line with alert(data) to check if the file is loading correctly?
                                  – thexebolud
                                  Nov 19 '11 at 14:09















                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                Make sure your JSON file does not have any trailing characters before or after. Maybe an unprintable one? You may want to try this way:



                                [{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]





                                share|improve this answer














                                Make sure your JSON file does not have any trailing characters before or after. Maybe an unprintable one? You may want to try this way:



                                [{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 19 '11 at 14:06

























                                answered Nov 19 '11 at 14:00









                                thexebolud

                                20219




                                20219








                                • 1




                                  JSON.parse('[{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]'); Works fine. ¿Have you try replacing that line with alert(data) to check if the file is loading correctly?
                                  – thexebolud
                                  Nov 19 '11 at 14:09
















                                • 1




                                  JSON.parse('[{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]'); Works fine. ¿Have you try replacing that line with alert(data) to check if the file is loading correctly?
                                  – thexebolud
                                  Nov 19 '11 at 14:09










                                1




                                1




                                JSON.parse('[{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]'); Works fine. ¿Have you try replacing that line with alert(data) to check if the file is loading correctly?
                                – thexebolud
                                Nov 19 '11 at 14:09






                                JSON.parse('[{"english":"bag","kana":"kaban","kanji":"K"},{"english":"glasses","kana":"megane","kanji":"M"}]'); Works fine. ¿Have you try replacing that line with alert(data) to check if the file is loading correctly?
                                – thexebolud
                                Nov 19 '11 at 14:09




















                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f8081701%2fi-keep-getting-uncaught-syntaxerror-unexpected-token-o%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







                                Popular posts from this blog

                                鏡平學校

                                ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                                Why https connections are so slow when debugging (stepping over) in Java?