Reputation: 115
I am creating Spring mvc app. I am submitting JSON string to controller through AJAX. What I want is to redirect the page to different JSP page.
Right now I am returning the view from controller but instead of redirecting it is returning response to previous AJAX request.
@RequestMapping("/hello")
public String hello() {
return "powerseries";
}
$(document).ready(function(){
$('#getData').click(function(){
var aa=JSON.stringify(answer);
$.ajax({
type: "POST",
url: "hello",
contentType: "application/json",
dataType:'json',
data:aa,
cache: false,
processData:false,
success: function(status){
console.log("Entered",status);
},
error:function(error){
console.log("error",error);
}
});
});
});
console.dir(answer);
Upvotes: 0
Views: 1053
Reputation: 3581
If you want to redirect to other jsp page , use redirect
inside controller method.
@RequestMapping("/hello")
public String hello() {
// return "powerseries";
return "redirect:powerseries";
}
// Add method to controller .
@RequestMapping("/powerseries")
public String returnPowerseries() {
return "powerseries";
}
or just use $("html").html(response);
if you want to entirely change your document html.
Upvotes: 0
Reputation: 4363
When you are using AJAX, your MVC should return a special JSON response.
eg:
@RequestMapping("/hello")
@ResponseBody
public Map hello() {
m.put('my_redirect', 'the new url');
return m;
}
then handle this response in your AJAX's handler. Use javascript's window.location.href = resp.my_redirect;
to go to the new url.
Upvotes: 1