Reputation: 49
I have a Spring Rest application with working API requests. I have some problems with the front-end side. jQuery get request doesn't work. Please help with it
I tried this code:
$(document).ready(function() {
$.ajax({
url: "http://localhost:8080/posts/"
}).then(function(data) {
$('.id').append(data.id);
$('.content').append(data.content);
});
});
Spring controller code:
@GetMapping("/posts")
public List<Post> getAllPosts() {
return postRepository.findAll();
}
Data from /post request:
[{"createdAt":"2019-03-08T04:00:00.000+0000","updatedAt":"2019-03-13T03:00:00.000+0000","id":321,"title":"Post 1","description":"Loreishe","content":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."},{"createdAt":"2019-03-08T04:00:00.000+0000","updatedAt":"2019-03-13T03:00:00.000+0000","id":323,"title":"Post 2","description":"Loreishe","content":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."},{"createdAt":"2019-03-08T04:00:00.000+0000","updatedAt":"2019-03-13T03:00:00.000+0000","id":324,"title":"Post 4","description":"kek","content":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."},{"createdAt":"2019-03-08T04:00:00.000+0000","updatedAt":"2019-03-13T03:00:00.000+0000","id":325,"title":"Post 3","description":"spring","content":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."}]
Upvotes: 0
Views: 378
Reputation: 178
$.ajax({
contentType: 'application/json',
url: "http://localhost:8080/posts/",
type: 'GET',
dataType: "json",
success: function(data) {
$('.id').append(data.id);
$('.content').append(data.content);
},
error: function(xhr, status, error) {
alert("Something went wrong");
}});
Upvotes: 1