0

How do I display the response of a call to a Spring MVC Controller returning HTML? In my Javascript code I make a (GET) call to my Spring Controller. From what I can make is that the response from the call is HTML. I guess I need to replace 'alert(response)' with Javascript to display the html.

My Javascript code:

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

@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.

1 Answers1

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 is the page you want to show on the success of get request. 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.

Hope that it works

  • the contents of response is a HTML document, returned from my Spring controller. I want to be redirected to a new page containing the response. Where does $(#ID) refer to? – Wai-kit Lo Jun 17 '19 at 14:52
  • Unfortunately, your solution messes up with my page, because the contents of table is not showing anymore. – Wai-kit Lo Jun 17 '19 at 16:12
  • just to check if I understand your solution correctly... I need to create a file so that the html from the response can be inserted in and then I redirect to that file? – Wai-kit Lo Jun 18 '19 at 10:59
  • Yes... You just need to insert that html content file in any div with that ID.. OR else you can just simply redirect to response.html – Chandra shekhar Jun 26 '19 at 03:52