2017-12-18 2 views
0

먼저 검색했지만 수정하지 않았습니다. 여기 내 jQuery 코드입니다. 여기 Ajax get undefined 편안한 API 목록

var rootUrl = "http://" + window.location.host + "/Blog/rest/posts"; 
 
var allPosts = function() { 
 
\t $.ajax({ 
 
\t \t type : 'GET', 
 
\t \t url : rootUrl, 
 
\t \t dataType : "json", 
 
\t \t success : renderAllPosts 
 
\t }); 
 
}; 
 

 
var renderAllPosts = function(data) { 
 
\t $.each(data, function(index, post) { 
 
\t \t alert(data); // returns [object Object] 
 
\t \t alert(post); \t // returns [object Object],[object Object] 
 
\t \t $('#allPosts').append(
 
\t \t \t \t '<a href=posts/' + post.ID + '><h3 id="animation_style">' 
 
\t \t \t \t \t \t + post.title + '</h3></a><hr>'); 
 

 
\t }); 
 
} 
 

 
$(document).ready(function() { 
 
// \t alert(window.location.host); 
 
\t allPosts(); 
 
});
이 난 단지 하나 개의 레코드가있을 때 rootUrl가 잘 작동 데이터베이스에 enter image description here

를 반환하는 데이터 ... 나는 아무 생각이 왜 경고 (게시물) 두 개의 객체를 반환합니다. 나는 그것이 경고 (데이터) 두 개의 객체를 반환해야한다고 생각합니다.

킵이 enter image description here

+0

나는 $ .each (data.data, function (in dex, post) 및 $ .each (data.dataList, function (index, post), 작동하지 않음) – Chase

답변

1

데이터를 받고, postModel라는 특성을 가지고, 당신이 변경해야합니다 :

var renderAllPosts = function(data) { 
    $.each(data, function(index, post) { 
     alert(data); // returns [object Object] 
     alert(post); // returns [object Object],[object Object] 
     $('#allPosts').append('<a href=posts/' + post.ID + '><h3 id="animation_style">'+ post.title + '</h3></a><hr>'); 
    }); 
} 

의 경우 :

var renderAllPosts = function(data) { 
    $.each(data.postModel, function(index, post) { 
     alert(data); // returns [object Object] 
     alert(post); // returns [object Object],[object Object] 
     $('#allPosts').append('<a href=posts/' + post.ID + '><h3 id="animation_style">'+ post.title + '</h3></a><hr>'); 
    }); 
} 

그 방법을, 당신은 사이클거야 게시물의 배열을 통해

+0

고마워요! 지금 일하고있다. 왜 데이터에 postModel이 있는지 설명해 주시겠습니까? Hibernate는 그것과 관련이 있는가? JDBC 코드만으로 대학에서이 작업을 수행했으며 완벽하게 작동했습니다. – Chase

+0

왜 'postModel'이 있는지 모르겠지만 업로드 한 그림에 응답을 표시하면 볼 수 있습니다. REST API에서 게시물 메소드를 확인해야합니다. –