2012-05-07 5 views
1

마침내 AJAX를 통해 데이터를 전달하고 JSON으로 반환하는 데 성공했습니다. 그러나 이제는 내 페이지에 표시하는 방법을 모르겠습니다. 내 모델이 실행 자바 스크립트 페이지에서 JSON 데이터를 표시

$(function() { 
    $("#selectable").selectable({ 
     selected: updatefilters, 
     unselected: updatefilters 
    }); 
    function updatefilters(ev, ui){ 
     // get the selected filters 
     var $selected = $('#selectable').children('.ui-selected'); 
     // create a string that has each filter separated by a pipe ("|") 
     var filters = $selected.map(function(){return this.id;}).get().join("\|"); 
     $.ajax({ 
      type: "POST", 
      url: 'updatefilters', 
      dataType: 'json', 
      data: { filters: filters }, 
      success: function(data){ 
       $('#board').replaceWith(data.content); 
      } 
     }); 
    } 
}); 

는, 그것은 불을 지르고에서 발견으로 다음 메아리 : 여기

내 JS입니다

[{"thread_id":"226","owner_id":"1","subject":"trying to create a HUGE body for thread testi","body":"Firstly, I think 45 characters is perfect for the heading. \n\nHowever, once the body gets huge, I assume that the \"preview\" is going to get a bit out of hand if not truncated. I have truncated code in place...but it doesn't seem to be working.\n\nI'll have to keep testing to ensure we can get it shrunk down to a few hundred characters so as to not completely screw up the page format\n\nyadda yadda yadda...yadda yadda yadda...and more yadda yadda yadda...\n\nBabble Babble Babble Babble Babble \n\nBabble Babble Babble ","timestamp":"2012-05-02 15:29:19","replystamp":"2012-05-02 15:29:53","last_post_id":"1","slug":"226-trying-to-create-a-huge-body-for-thread-testi","replies_num":"1","views":"19"},{"thread_id":"219","owner_id":"3","subject":"testing icons","body":"hellow world","timestamp":"2012-05-01 11:37:08","replystamp":"2012-05-01 11:37:08","last_post_id":"3","slug":"219-testing-icons","replies_num":"0","views":"3"},{"thread_id":"212","owner_id":"1","subject":"This thread is based off entertainm","body":"Yay","timestamp":"2012-04-25 14:07:55","replystamp":"2012-04-25 14:07:55","last_post_id":"1","slug":"212-this-thread-is-based-off-entertainment","replies_num":"0","views":"4"}] 

데이터 사업부 "보드"를 교체하는 방법에 대한 조언 반환 된 JSON 배열에서 매우 도움이 될 것입니다!

답변

1

에서 값에 액세스 할 수 있습니다. 다음과 같이 작성할 수 있습니다.

success: function(data){ 
    var html = "<table>"; 
    for (i=0 ; i< data.length ; i++) 
    { 
     html += "<tr><td>" + data[i].subject + "</td><td>" + data[i].body + "</td></tr>"; 
    } 
    html += "</table"; 
    $('#board').html(html); 
} 

아이디어를 얻을 수 있습니다. 또한 더 나은 솔루션을 조사 할 시간이 있다면 템플릿 라이브러리 mustache.js을 조사하는 것이 좋습니다. 그렇게하면 템플릿을 데이터에서 분리하고 Javascript를 사용하여 HTML과 데이터의 추악한 연결을 제거 할 수 있습니다. (좀 더 고급 솔루션 일 수도 있고, 간단하게 유지하는 것이 좋습니다.) Mustache.js 엔진을 사용하면 위와 같이 작성할 수 있습니다.

var template = "{{#item}}<tr><td>{{subject}}</td><td>{{body}}</td></tr>{{/item}}"; 
var html = "<table>" + mustache.render(template, data) + "</table>"; 
$("#board").html(html); 
+0

나는 orqlally mySQL 쿼리에서 가져온 데이터를 표시하는 뷰를 가지고있었습니다. 이보기를 사용하여이 데이터를 표시 할 수있는 사람이 있습니까? 그것의 많은 HTML ... 그리고 javascript에 넣는 것은 매우 어렵습니다. –

1
success: function(data){ 
$.each(data, function(index, value) { 
    $('#board').append(index + ': ' + value + '<br/>'); 
}); 
} 

또한 직접의 당신은 제목과 본문이있는 테이블의 데이터를 표시 할 가정 해 봅시다 JSON

success: function(data) { 
    $('#board').append('Thread Subject' + data.subject); 
} 
+0

고마워요! 그것의 마지막 무언가. 0 : [객체 객체] 1 : [객체 객체] 2 : [객체 객체] –

+0

console.log (data)를 성공에 제공하고 여기에 방화 광 물이 인쇄하는 것을 붙여 넣을 수 있습니까? –

+0

나는 그 질문에 올린 것과 똑같은 것을 보여줄 것이라고 믿는다 (두 번째 코드 포스트). 임씨는 화제가 아니며, 당신이 물어 보는 것을 성취하는 방법을 확신하지 못했습니다. –

관련 문제