2012-08-09 102 views
0

JSON不能正常工作。這是下面的代碼。Json不按鈕點擊工作?

ajax.aspx文件: -

<form id="form1" runat="server"> 
    <div id="dictionary"> 
</div> 
<div class="letters"> 
<div class="button" id="letter-a"> 
<h3>A</h3> 
<button type="button">Load</button> 
</div> 
<div class="button" id="letter-b"> 
<h3>B</h3> 
<button type="button">Load</button> 
</div> 
<div class="button" id="letter-c"> 
<h3>C</h3> 
<button type="button">Load</button> 
</div> 
<div class="button" id="letter-d"> 
<h3>D</h3> 
<button type="button">Load</button> 
</div> 
</div> 
</form> 

這是b.json文件:_

[ 
{ 
"term": "BACCHUS", 
"part": "n.", 
"definition": "A convenient deity invented by the ancients as an 
excuse for getting drunk.", 
"quote": [ 
"Is public worship, then, a sin,", 
"That for devotions paid to Bacchus", 
"The lictors dare to run us in,", 
"And resolutely thump and whack us?" 
], 
"author": "Jorace" 
}, 
{ 
"term": "BACKBITE", 
"part": "v.t.", 
"definition": "To speak of a man as you find him when he can't 
find you." 
}, 
{ 
"term": "BEARD", 
"part": "n.", 
"definition": "The hair that is commonly cut off by those who 
justly execrate the absurd Chinese custom of shaving the head." 
}, 
] 

ajax.js文件: -

$(document).ready(function() { 
    $('#letter-a button').click(function() { 
     $('#dictionary').load('html_ajax.htm'); 
     alert('Loaded!'); 
    }); 
    $('#letter-b button').click(function() { 
     $.getJSON('b.json', function (data) { 
      $('#dictionary').empty(); 
      $.each(data, function (entryIndex, entry) { 
       var html = '<div class="entry">'; 
       html += '<h3 class="term">' + entry['term'] + '</h3>'; 
       html += '<div class="part">' + entry['part'] + '</div>'; 
       html += '<div class="definition">'; 
       html += entry['definition']; 
       if (entry['quote']) { 
        html += '<div class="quote">'; 
        $.each(entry['quote'], function (lineIndex, line) { 
         html += '<div class="quote-line">' + line + '</div>'; 
        }); 
        if (entry['author']) { 
         html += '<div class="quote-author">' + entry['author'] + 
'</div>'; 
        } 
        html += '</div>'; 
       } 
       html += '</div>'; 
       html += '</div>'; 
       $('#dictionary').append($(html)); 
      }); 




     }); 
    }); 
}); 

點擊按鈕是工作,但B不是。我是Ajax的新手。所以我希望我失去了一些東西。

哪裏可能是錯誤。

感謝

回答

1

根據jsonlint您b.json文件無效。 您應該刪除最後一個逗號使其有效。

+0

@ user1544975:參照你的第二個問題,你所得到的錯誤,因爲「定義」的值應該是所有在像這樣一行:「古人發明爲喝醉的藉口方便的神。」 – effectica 2012-08-09 12:30:07

+0

Thanks.its現在工作。 – user1544975 2012-08-09 12:37:59