Trying to parse JSON with underscore (Unexpected token on Chrome)

11,051

It turned out the problem was with url parameter.

 url : "users.json"
 url: "/users.json"

Error thrown by Chrome:

Uncaught SyntaxError: Unexpected token , 

After an hour of troubleshooting, i found out: Chrome has a bug on caching GET requests.

It can be fixed by setting

cache: false 

on my Ajax call!

Also making a directory and calling that directory on url seems to be working.

url : "json/users.json"

Thanks to those who tried to help.

Share:
11,051
WhatisSober
Author by

WhatisSober

If you are doing the same thing thrice, make a script !!!

Updated on June 13, 2022

Comments

  • WhatisSober
    WhatisSober almost 2 years

    Hello I am trying to get a grasp on underscore.js

    i have a json file as follows:

    [
    {
        "name":"rocky",
        "last-updated": "Yesterday", 
        "age":"32"
    },
    {
        "name":"annie",
        "last-updated": "Today",
         "age":"31"
    }
    ]
    

    And a javascript function:

        function getNames() {
            var users = $.ajax({
                url : "users.json",
                async : false
            });
    
            var names = _.map(JSON.parse(users.responseText),
                function(user) {
                    return user.name
                });
    
            return names;
    
        }
    

    It works fine on IE but on Chrome, it throws me:

    Uncaught SyntaxError: Unexpected token , 
    

    on this line:

    var names = _.map(JSON.parse(users.responseText),function(user) {return user.name});
    

    As far as I know this error is because of trying to parse object not the JSON string. Am i right? How do I solve this? It works on IE?

    Thank you!