How do I send data via an Ajax Query to a Controller and then open a new JSP page as the Controller's return type?

178
March 23, 2018, at 00:04 AM

This is a simplified version of a problem I'm having. How do I send data to a Spring Boot Controller via an AJAX Query and then open a new JSP page? When I send data to the url in the AJAX Query to the matching URL in my controller class it seems to run the code within that method but not open the JSP (or return type).

For example if I were to send the following data from an Ajax Query

var hello = "Hello World!";
$.ajax({
         type: "POST",
         url: "/message1",
         data: { 
                 message: hello, 
               }, 
         datatype: 'json'
      });
}

To this method in my Controller class

@RequestMapping(value = "/message1", method = RequestMethod.POST)
public String MessageReceiver(@RequestParam("message")String message, 
                              BindingResult bindingResult,              
                              Model model) {
    System.out.println(message);
    return "NewPage";
}

"Hello" will print in the console but the JSP page NewPagewill fail to open and will prompt no errors?

Usually if I just call the url in the controller (for example /message1) via a href link, or button etc the NewPage JSP page will open. It seems that the AJAX query is missing something. Do I have to update the URL in the Ajax Query to something similar to /message1/NewPage (tried this, didn't work), or add something to the controller because the NewPage JSP page will not open.

Answer 1

Add the success callback function to your ajax like so:

$.ajax({
         type: "POST",
         url: "/message1",
         data: { 
                 message: hello, 
               }, 
         datatype: 'json',
         success: function(data){
               if(data === "NewPage"){
                    //Open new page
               }
         }
      });
}
Rent Charter Buses Company
READ ALSO
Making responsive web site with cross domain DB access

Making responsive web site with cross domain DB access

I planned to start making an average sized RWD web site with ability to access DB data via cross domain optionPlan also include nice and smooth access with classic computers and tablets, mostly 10" diagonals

190
Storing video files using jQuery

Storing video files using jQuery

I am working on a project where I will provide the facility to the user to store the video files into the databaseSo later on the application will show all of the uploaded video files

171
toArray() and keeping data attributes

toArray() and keeping data attributes

How is it possible to pass an array of elements as well as keeping the data attributes using jQuery? currently I've got the following:

194
How to call php function after using jquery sortable function

How to call php function after using jquery sortable function

I am trying to call my php controller function using javascript and jquery but I am not reaching the function

245