从Spring Boot应用程序获取jQuery请求

时间:2019-04-15 19:46:38

标签: jquery spring

我有一个Spring Rest应用程序,带有有效的API请求。前端方面有些问题。 jQuery get请求不起作用。请帮忙

我尝试了以下代码:

$(document).ready(function() {
    $.ajax({
        url: "http://localhost:8080/posts/"
    }).then(function(data) {
        $('.id').append(data.id);
        $('.content').append(data.content);
    });
});

Spring控制器代码:

@GetMapping("/posts")
public List<Post> getAllPosts() {
    return postRepository.findAll();
}

/ post请求中的数据:

[{"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."}]

1 个答案:

答案 0 :(得分:1)

$.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");
        }});