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