Spring MVC 3, forwarding not working when using Ajax
-
27-10-2019 - |
Question
I have very interesting problem. I am making log in page for my web app and I am sending login request via AJAX. If success I want to forward user to another page.
It seems that his is what happens. I send Ajax request, controller forwards me to need view (I see log in debug mode) but I stay on the same page, since I assume the page is waiting for AJAX response and for that reason forwarding does not happen.
I think this is also wrong way to approach this but since I am new to this don't know better. How can I log in and and forward user to next page.
Thank you.
Here is my code:
JS Code:
Page.authenticate = function() {
$.ajax({
url: "/login/authenticate/" + $('#username').val() + "/" + $('#password').val(),
type: "GET",
success: function(poi){
// alert("nesto");
}
});
return true;
}
Controller Class:
@Controller
public class LoginPageController {
private Logger logger = Logger.getLogger(this.getClass());
@Autowired
private UserManagement userManagement;
@RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) {
if (userManagement.authenticateUser(userName, password)) {
String forward = "forward:/login/success";
return forward;
} else {
model.addAttribute("errorMessage", "Invalid Username/Password, please try again!");
return "/";
}
}
}
Solution
You need to response within @ResponseBody
Annotation if you are using AJAX.
@RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) {
if (userManagement.authenticateUser(userName, password)) {
String forward = "forward:/login/success";
return forward;
} else {
String forward = "forward:/login/error?message=Invalid Username/Password, please try again!";
return forward;
}
}
@RequestMapping(value="/login/success", method=RequestMethod.GET)
@Responsebody
public String handleMySuccessRedirect() {
return "Logged In successfully"
}
@RequestMapping(value="/login/error", method=RequestMethod.GET)
@Responsebody
public String handleMyExceptionOnRedirect(@RequestParamter("message") String message) {
return message;
}
Update:
@RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET)
@ResponseBody
public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) {
if (userManagement.authenticateUser(userName, password)) {
String response = "Logged Successfully";
return response;
} else {
String response = "Invalid Username/Password, please try again!";
return response;
}
}
OTHER TIPS
There are a couple of things you can do here:
Don't return a view from your controller, instead return json, based on the response in json, set the location appropriately -
window.location = 'home.action'
- here is an example using ext-jsLet the login page perform a full fledged post, not an AJAX post.