如何显示从AJAX调用Spring控制器方法的响应?

问题描述 投票:0回答:1

如何显示对返回HTML的Spring MVC Controller的调用的响应? 在我的Javascript代码中,我对我的Spring Controller进行了(GET)调用。 我可以做的是来自呼叫的响应是HTML。 我想我需要用Javascript替换'alert(response)'以显示html。

我的Javascript代码:

     $('#parcelsTable').on( 'click', 'tr', function () {
         var data = table.row( this ).data();

         $.ajax({
             url:"/parcel/showFormForUpdate",
             type:"GET",
             data:{parcelId:data.parcelId},
             success: function(response){
                alert(response) 
             }
         });
     } );

我在Spring中的控制器代码:

@GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("parcelId") int theId, Model theModel) {

    Parcel theParcel = parcelService.findById(theId);
    theModel.addAttribute("theParcel", theParcel);
    return "parcel-form";
}

这里“parcel-form”是模板的名称。

javascript ajax spring-mvc
1个回答
0
投票
     $('#parcelsTable').on( 'click', 'tr', function () {
         var data = table.row( this ).data();

         $.ajax({
             url:"/parcel/showFormForUpdate",
             type:"GET",
             data:{parcelId:data.parcelId},
             success: function(response){
                $.get(response.html, function(data, status){
                $("#ID").html(data);
                }); 
             }
         });
     } );

response.html是您希望在get请求成功时显示的页面。 只需向response.html文件或任何模板文件发出get请求,并将此文件放在要显示它的任何div中。

希望它有效

© www.soinside.com 2019 - 2024. All rights reserved.