2

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!

4

1 回答 1

3

原来问题出在url参数上。

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

Chrome 抛出的错误:

Uncaught SyntaxError: Unexpected token , 

经过一个小时的故障排除后,我发现: Chrome 在缓存 GET 请求方面存在错误。

可以通过设置来修复

cache: false 

在我的 Ajax 通话中!

还创建一个目录并在url上调用该目录似乎正在工作。

url : "json/users.json"

感谢那些试图提供帮助的人。

于 2013-10-17T13:24:04.413 回答