0

我正在尝试将.json数据加载到我的网络应用程序中,但它永远无法成功。

它总是返回错误函数。

这是我的.js文件:

 $(document).ready(function () {
 var output = $('#stage');

 $.ajax({
     url: 'http://www.haverhill-ps.org/ios/app/data/calendar-json.json',
     dataType: 'json',
     timeout: 5000,
     success: function (data) {
         $.each(data, function (i, item) {
             var eventInfo = '<h1>' + item.month + '</h1>' + '<p>' + item.date + '<br>' + item.time + '</p>';

             output.append(eventInfo);
         });
     },
     error: function () {
         output.text('There was an error loading the data.');
     }
 });
});

这是json数据:

{
    "month": "June", //November
    "date": "10", //10
    "time": "5PM", //5PM
}, {
    "month": "July", //November
    "date": "4", //10
    "time": "1PM", //5PM
}

然后,在我的 html 中,我有一个 div 设置:

<div id="stage">Run here...</div>  
4

2 回答 2

1

返回的数据不是有效的 json。

您丢失了结果,[并且] 注释在 json 中无效。每个对象的,最后一个元素末尾的 也是无效的。您可以在例如http://jsonlint.com/验证您的 json

一个有效的 json 看起来是这样的:

[{
    "month": "June",
    "date": "10",
    "time": "5PM"
}, {
    "month": "July",
    "date": "4",
    "time": "1PM"
}]
于 2013-04-05T13:21:32.807 回答
0

可能你想看看是什么错误,这样做

error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
于 2013-04-05T13:20:37.270 回答