2012-08-09 3 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)); 
      }); 




     }); 
    }); 
}); 

버튼 A의 클릭은 일하지만 B는 그렇지 않습니다. 나는 아약스 물건에 초보자입니다. 그래서 나는 뭔가를 놓치기를 바랍니다.

오류 일 수 있습니다.

감사

답변

1

jsonlint에 따라 b.json 파일이 유효하지 않습니다. 유효하도록 마지막 쉼표를 제거해야합니다.

+0

@ user1544975 : 두 번째 질문과 관련하여 "정의"의 값은 모두 한 줄에 있어야하기 때문에 오류가 발생합니다. "술 취하는 변명으로 고대인이 고안 한 편리한 신" – effectica

+0

감사합니다. 지금 작동 중입니다. – user1544975