javascript spring modelandview重定向在jquery ajax调用中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19375896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:22:06  来源:igfitidea点击:

spring modelandview redirect not working in jquery ajax call

javascriptjqueryajaxspringspring-mvc

提问by pappu_kutty

I am returing modelview attrbute form below controller

我正在返回控制器下方的模型视图属性表单

@RequestMapping(value = "/signin", method = RequestMethod.POST)
    public @ResponseBody
    ModelAndView loginUser(@RequestParam(value = "userName") String userName,
            @RequestParam(value = "password") String password,
            ModelAndView model) {
     on success login

       return new ModelAndView("redirect:/another controller here");
     on failure login 

           return new ModelAndView("same page");

i have jquery ajax call which invokes this controller, when successful login i want to user to take another page, but my response comes to the same page instead of redirecting to another page.

我有调用此控制器的 jquery ajax 调用,当成功登录时,我希望用户访问另一个页面,但我的响应来自同一页面,而不是重定向到另一个页面。

help needed

需要帮助

EDIT - I am using URL based view resolver and Apache tiles for view rendering

编辑 - 我使用基于 URL 的视图解析器和 Apache 磁贴进行视图渲染

回答by Will Keeling

Assuming I'm understanding your problem, the redirect will take place within the context of the AJAX call - so the JQuery AJAX code will follow the redirect and receive the response it gets back from /another controller here. This won't trigger a full page refresh but the response will be available by the JQuery AJAX code for rendering in the current page.

假设我理解您的问题,重定向将在 AJAX 调用的上下文中发生 - 因此 JQuery AJAX 代码将遵循重定向并接收它从/another controller here. 这不会触发整个页面刷新,但响应将由 JQuery AJAX 代码用于在当前页面中呈现。

To change the whole page on successful login, you may be better to not use AJAX for this screen.

要在成功登录时更改整个页面,您最好不要在此屏幕上使用 AJAX。

Alternatively, you could return some indicator to tell JQuery to redirect. For example:

或者,您可以返回一些指示符来告诉 JQuery 重定向。例如:

@RequestMapping(value = "/signin", method = RequestMethod.POST)
    public @ResponseBody
    ModelAndView loginUser(@RequestParam(value = "userName") String userName,
        @RequestParam(value = "password") String password,
        ModelAndView model) {

     on success login
       ModelAndView modelAndView = new ModelAndView("same page");
       modelAndView.addObject("redirectUrl", "/another controller here");
       return modelAndView;

     on failure login 
       return new ModelAndView("same page");

And then have some Javascript in same pagethat looks for the presence of redirectUrland triggers a full page redirect if present.

然后在其中使用一些 Javascriptsame page查找是否存在redirectUrl并触发完整页面重定向(如果存在)。

var redirect = '${redirectUrl}';
if (redirect) {
    window.location.replace(redirect);
}