3

我有一个 JS(data.js) 文件,其中的数据为

var Content = {
    Page1: {
        Tiles: [
        { "id": "1",
            "className": "home-test-class",
            "tileType": "widget",
            "tileColor": "red",
            "Bookmark": : {
                "Icon": "iconset", 
                "Class": "dummytext", 
                "Content": "Dummy Content",
                "URL": ""
            },
            "Tile": false,
            "SmallWidget": false,
            "Widget": false
        },
        { 
        "id": "1",
            "className": "title-class",
            "tileType": "widget",
            "tileColor": "red",
            "Bookmark": false,
            "Tile": false,
            "SmallWidget": false,
            "Widget": false
        },
        ]
    }
}

我创建了一个 INIT 方法来使用这些数据

Init = function () {
        $.ajax({
            url: 'scripts/data.js',
            dataType: "json",
            contentType: 'application/json; charset=utf-8'
        }).done(function (data) {
            $.doTimeout(1000, function () {
                console.log(data);
                LoadViewData(data);
            });

        }).fail(function (request, error) {
            //Handle Failures here.
            alert('Error' + error);
        });
        ko.applyBindings(this);
    },

它给了我一个 JsonParse 错误。

我正在使用敲除将数据绑定到 UI。当我使用 Fiddler 并检查响应时,它说“所选响应不包含 vaid JSON 文本

请让我知道我该如何解决。

4

1 回答 1

4

这是因为您返回的是 Javascript 文件,而不是 JSON 数据。以这种方式使用 Javascript 文件是没有意义的。

如果您将文件更改为包含 JSON 字符串(下面的示例),并假设您的其余$.ajax()调用没有问题,它将起作用。请注意,正确的 JSON字符串将所有名称括在双引号中。此外,如果您通过验证器运行 JSON ,您会发现它存在一些问题(我已经在示例中修复了它们)。

{
    "Page1": {
        "Tiles": [
            {
                "id": "1",
                "className": "home-test-class",
                "tileType": "widget",
                "tileColor": "red",
                "Bookmark": {
                    "Icon": "iconset",
                    "Class": "dummytext",
                    "Content": "Dummy Content",
                    "URL": ""
                },
                "Tile": false,
                "SmallWidget": false,
                "Widget": false
            },
            {
                "id": "1",
                "className": "title-class",
                "tileType": "widget",
                "tileColor": "red",
                "Bookmark": false,
                "Tile": false,
                "SmallWidget": false,
                "Widget": false
            }
        ]
    }
}
于 2012-07-20T06:17:36.170 回答