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

javascript - Trying to parse JSON with underscore (Unexpected token on Chrome) - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share Improve this question edited Oct 17, 2013 at 12:14 WhatisSober asked Oct 17, 2013 at 12:02 WhatisSoberWhatisSober 9982 gold badges13 silver badges32 bronze badges 5
  • i thought json's opening and closing brackets where {}, and [] where for array – gwillie Commented Oct 17, 2013 at 12:03
  • 3 Thats not underscore thats a hyphen! – meda Commented Oct 17, 2013 at 12:04
  • Avoid - chars in you key's – Manuel van Rijn Commented Oct 17, 2013 at 12:04
  • @gwillie:I replaced [] with {} on json file, still same error – WhatisSober Commented Oct 17, 2013 at 12:09
  • @meda: you mean _.map ? i am sure its underscore! – WhatisSober Commented Oct 17, 2013 at 12:10
Add a ment  | 

1 Answer 1

Reset to default 3

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.

发布评论

评论列表(0)

  1. 暂无评论