簡體   English   中英

如何顯示從AJAX調用Spring控制器方法的響應?

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

如何顯示對返回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”是模板的名稱。

     $('#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中。

希望它有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM