0

我从 js 文件(“scripts/data.js”)中获取一些数据。对我来说,它看起来像是有效的 JSON,但它正在被我的 jQuery.ajax() 调用中的错误处理程序捕获/处理。

这是显示的错误消息:

Invalid JSON: var data = [
{
    'id': '1',
    'firstName': 'Megan',
    'lastName': 'Fox',
    'picture': 'images/01.jpg',
    'bio': 'Megan Denise Fox was born May 16, 1986 in Rockwood, Tennessee. She has one older sister. Megan began her training in drama and dance at the age of 5 and, at the age of 10, moved to Florida where she continued her training and finished school. She now lives in Los Angeles, California. Megan began acting and modeling at the age of 13 after winning several awards at the 1999 American Modeling and Talent Convention in Hilton Head, South Carolina. Megan made her film debut as Brianna Wallace in the Mary-Kate Olsen and Ashley Olsen film, Holiday in the Sun (2001) (V). Her best known role is as Sam Witwicky\'s love interest Mikaela Banes in the first two installments of the Transformers series: Transformers (2007) and Transformers: Revenge of the Fallen (2009).'
},
{
    'id': '2',
    'firstName': 'Robert',
    'lastName': 'Pattinson',
    'picture': 'images/02.jpg',
    'bio': 'Robert Pattinson was born on May 13, 1986, in London, England. He enjoys music and is an excellent musician, playing both the guitar and piano.\n\nWhen Robert was 15, he started acting in amateur plays with the Barnes Theatre Company. Afterward, he took screen role like Curse of the Ring (2004) (TV) (Kingdom of Twilight) as Giselher.\n\nIn 2003, Robert took on the role of Cedric Diggory in Harry Potter and the Goblet of Fire (2005). He got his role a week later after meeting Mike Newell in late 2003.\n\nHe has since been cast as Edward Cullen in the highly-anticipated film, Twilight (2008/I). His music will also be heard in the film. Additionally, Robert has completed upcoming roles as Salvador Dal� in Little Ashes (2008) and Art in How to Be (2008).'
}]

这是进行调用的代码位:

$(document).ready(function () {
        $.ajax({
            url: "scripts/data.js",
            data: "nocache=" + Math.random(),
            type: "GET",
            dataType: "json",
            success: function (source) {
                data = source;
                showInfo();
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("thrownError");
                                            }
        });
    });
4

3 回答 3

2
  1. 它开始于var data =
  2. 字符串用 分隔,'但必须用"

您应该使用http://jsonlint.com/

于 2013-09-07T07:47:36.427 回答
1

您在这里拥有的不是JSON,而是 JavaScript。

jQuery 也能够获取和执行这些脚本。$.ajax将命令中的数据类型更改为dataType: "script"或使用快捷函数$.getScript

于 2013-09-07T07:49:10.480 回答
-1

尝试取下var data =. JSON 不是 javascript,也没有赋值。

尝试将单引号更改为双引号。

这是一个可以提供帮助的在线 JSON 验证器: http ://www.freeformatter.com/json-validator.html

于 2013-09-07T07:46:32.850 回答