2012-07-20 71 views
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

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

如果你改变了你的文件,以只是包含一个JSON字符串(下面的例子),并假设你$.ajax()通话剩下的没有问题,请注意,proper JSON字符串将所有名称都包含在双引号中。另外,如果您通过运行JSON 10,你会发现它有一些问题(我已经在示例中解决了它们)。

{ 
    "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 
      } 
     ] 
    } 
} 
+1

感谢洛特,为您的答案..这种方式晶莹剔透,非常有帮助。 – Shubh 2012-07-20 08:31:35