Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
746 views
in Technique[技术] by (71.8m points)

spring-boot - 我收到表单数据后,Spring MVC返回重定向不起作用(Spring MVC return redirect not working once I receive form data)

Can anyone review why the return redirect is not working?

(谁能回顾为什么返回重定向不起作用?)

My "/" route displays a form - I am using jQuery to stringify the user inputs and sending it to "/login" and successfully receiivng it (verified using system.out.println) - afte that, when I try to redirect to /chat/{username}, it doesnt work.

(我的“ /”路由显示一个表单-我正在使用jQuery来对用户输入进行字符串化,然后将其发送到“ / login”并成功接收到它(已使用system.out.println进行了验证)-之后,当我尝试重定向至/ chat / {username},它不起作用。)

When I manualy visit /chat/john for example it works.

(例如,当我手动访问/ chat / john时,它可以工作。)

Even a hardcoded redirect,return "redirect:/chat/John" isnt working.

(即使是硬编码的重定向,返回“ redirect:/ chat / John”也不起作用。)

In the browser's web console Network tab, I can see that it sends the traffic to /chat/John" but the webpage does not refresh.

(在浏览器的Web控制台的“网络”选项卡中,可以看到它将流量发送到/ chat / John”,但是网页没有刷新。)

When I try to redirect after a successful Ajax call in my JavaScript file, it works.

(当我在JavaScript文件中成功调用Ajax之后尝试重定向时,它可以工作。)

However, the redirect works, when I have "both" return redirect in my Spring controller, and success:function(data){window.location.href='/chat'/{username}} in my Ajax.

(但是,当我在Spring控制器中“同时”返回重定向,并且在我的Ajax中成功执行了function:(data){window.location.href ='/ chat'/ {username}}时,重定向有效。)

Which is wierd - not sure why I need both for the redirect to work

(哪个很奇怪-不确定为什么我需要同时进行重定向才能工作)

@RequestMapping(headers = "Content-Type=application/json",value="/login", method=RequestMethod.POST)
    public String message(@RequestBody List<Map<String, String>> formData, Model model) {
        String username=null;
        Map<String, String> formInputs = new HashMap<String, String>();


        for (Map<String, String> formInput : formData) {
            formInputs.put(formInput.get("name"), formInput.get("value"));
            username = formInput.get("value");
        }

        return "redirect:/chat" + username;


    }

    @RequestMapping(value="/chat/{username}",method=RequestMethod.GET)
    public String chat(@PathVariable("username") String username, Model model) {
        model.addAttribute("username", username);
        return "message.jsp";
    }

Below is my JavaScript file where I post the form data to "/login"

(以下是我的JavaScript文件,我将表单数据发布到“ / login”)

 $(document).ready(function(){
        $("form").on('submit',function(e){
            e.preventDefault();
            $.ajax({
                url:'/login',
                type:'POST',
                data: JSON.stringify($("form").serializeArray()),
                contentType:'application/json',
                success:function(data){window.location.href='/chat'}
            })

            return false;
        })

   })
   })```
  ask by Shyam Gupta translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to understand that the Server-side redirect is different from your windows.location.href .

(您需要了解服务器端重定向与windows.location.href不同。)

Firstly when you say redirect:/some-url in the server it sends a corresponding HTTP Status method (302) along with the URL to which it was redirected to the browser (jQuery API).

(首先,当您在服务器中说redirect:/some-url时,它将发送相应的HTTP Status方法(302)以及将其重定向到浏览器的URL(jQuery API)。)

The browser (jQuery API) then does a call to the redirected URL and it gets the HTML of the redirected url which is available as a parameter to the success callback.

(然后,浏览器(jQuery API)调用重定向的URL,并获取重定向的URL的HTML,该HTML可以用作success回调的参数。)

So all this is happening asynchronously and finally, in the success callback you get the HTML of the redirected URL.

(因此,所有这些都是异步发生的,最后,在success回调中,您将获得重定向URL的HTML。)

But if you want to reload the page, then you don't need a redirect from Server.

(但是,如果要重新加载页面,则不需要从Server重定向。)

The server method can just send a Success to the Ajax call and then in the success callback handler you can call window.location.href to reload a new page.

(服务器方法可以仅将成功发送给Ajax调用,然后在success回调处理程序中,可以调用window.location.href重新加载新页面。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...