Skip to content Skip to sidebar Skip to footer

Is There A Way That I Can Redirect My Page (jsp) To Another Page (jsp) After An Ajax Post Call In Springmvc

Basically my scenario is I am trying to send a list of 3 objects as a string to my controller by using ajax post as shown below. JavaScript function for AJAX call: $.ajax({ typ

Solution 1:

You can't go to the page which you recived in the response to the POST request. But your can add success to ajax:

$.ajax({
type: 'POST',
dataType: 'json',
url: "ajaxEditFormUpdate",
data: JSON.stringify(newData),
beforeSend: function(xhr) { 
    xhr.setRequestHeader("Accept", "application/json");  
    xhr.setRequestHeader("Content-Type", "application/json");  
},
success: function (response) {
    window.location.href = '/content/review';
}
});

You controller now will be looking like this:

@RequestMapping(value = "ajaxEditFormUpdate", method = RequestMethod.POST)@ResponseBodypublic ResponseEntity<Void> handleResponse(@RequestBody String records) {
        System.out.println(records);
        return new ResponseEntity<>(HttpStatus.OK);
    }

but now you also need make new controller with GET to return view for /content/review page

p.s. Another variant seems to be a hack if you still want to render page without changing any controllers, your success must will be like this:

success: function (response) {
        document.open();
        document.write(response);
        document.close();
    }

But this approach is not recommended.

Post a Comment for "Is There A Way That I Can Redirect My Page (jsp) To Another Page (jsp) After An Ajax Post Call In Springmvc"