简体   繁体   English

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

[英]How to show response from AJAX call to Spring controller method?

How do I display the response of a call to a Spring MVC Controller returning HTML? 如何显示对返回HTML的Spring MVC Controller的调用的响应? In my Javascript code I make a (GET) call to my Spring Controller. 在我的Javascript代码中,我对我的Spring Controller进行了(GET)调用。 From what I can make is that the response from the call is HTML. 我可以做的是来自呼叫的响应是HTML。 I guess I need to replace 'alert(response)' with Javascript to display the html. 我想我需要用Javascript替换'alert(response)'以显示html。

My Javascript code: 我的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) 
             }
         });
     } );

My controller code in Spring: 我在Spring中的控制器代码:

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

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

Here "parcel-form" is the name of a template. 这里“parcel-form”是模板的名称。

     $('#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 is the page you want to show on the success of get request. response.html是您希望在get请求成功时显示的页面。 Just make a get request to the response.html file or any template file and put this file in any div where you want to show it. 只需向response.html文件或任何模板文件发出get请求,并将此文件放在要显示它的任何div中。

Hope that it works 希望它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM