Spring MVC 3,使用 Ajax 时转发不起作用
我有一个非常有趣的问题。我正在为我的网络应用程序制作登录页面,并通过 AJAX 发送登录请求。如果成功我想将用户转发到另一个页面。
看来他的遭遇就是这样。我发送 Ajax 请求,控制器将我转发到需要的视图(我在调试模式下看到日志),但我停留在同一页面上,因为我假设该页面正在等待 AJAX 响应,因此转发不会发生。
我认为这也是错误的处理方法,但由于我是新手,所以不知道更好。我如何登录并将用户转发到下一页。
谢谢。
这是我的代码:
JS代码:
Page.authenticate = function() {
$.ajax({
url: "/login/authenticate/" + $('#username').val() + "/" + $('#password').val(),
type: "GET",
success: function(poi){
// alert("nesto");
}
});
return true;
}
控制器类:
@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 "/";
}
}
}
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 "/";
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 AJAX,则需要在
@ResponseBody
注解内进行响应。更新:
You need to response within
@ResponseBody
Annotation if you are using AJAX.Update:
您可以在此处执行以下操作:
不要从控制器返回视图,而是返回 json,根据 json 中的响应,适当设置位置 -
window.location = 'home. action'
- 这是一个 使用的示例ext-js让登录页面执行完整的发布,而不是 AJAX 发布。
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.