简体   繁体   English

在 Spring 中的 POST 请求后进行重定向

[英]Making a redirect after a POST Request in Spring

I have a button with this onClick function in JavaScript which makes a post request to my Spring server with some data.(I'm not sending any data because I'm trying to solve this problem before that) I have a button with this onClick function in JavaScript which makes a post request to my Spring server with some data.(I'm not sending any data because I'm trying to solve this problem before that)

async function redirectToModel(model, make){
    const url = 'http://localhost:8080/carmodel';

    const data = {
            make: getCodeName(make),
            model: getCodeName(model)
        };

    const params = {
            mode: "no-cors",
            headers:{
                "Content-Type": "application/json"
            },
            method: "POST"
        }
    await fetch(url, params);
}

My Spring controller looks like this我的 Spring controller 看起来像这样

@Controller
public class NController {
    @GetMapping("/carmodelget")
    public String tester(Model model){
        System.out.println("get request");

        model.addAttribute("name", "test");
        return "carmodel";
    }

    @PostMapping("/carmodel")
    public String aaa(){
        System.out.println("post request");

        return "redirect:/carmodelget";
    }
}

What I'm trying to do is redirect the user from the page with the mentioned button to carmodel.html but I also want to send data to that carmodel.html, because it's a thymeleaf template.我要做的是将用户从带有提到的按钮的页面重定向到 carmodel.html 但我也想将数据发送到该 carmodel.html,因为它是 ZDB38BD6F666CEF7A2F9381F39EB7 模板。

The problem is that I get both "post request" and "get request" printed to the console in Spring, but the redirect never actually happens.问题是我在 Spring 中将“发布请求”和“获取请求”都打印到控制台,但重定向实际上从未发生。 What am I doing wrong?我究竟做错了什么?

Spring boot gives me an error: HttpRequestMethodNotSupportedException: Request method 'GET' not supported Spring 启动给我一个错误:HttpRequestMethodNotSupportedException:不支持请求方法'GET'

And the browser console gives me an error: net::ERR_ABORTED 405浏览器控制台给了我一个错误:net::ERR_ABORTED 405

You can not redirect within a ajax call.您不能在 ajax 调用中重定向。

Try to send the redirect URL back as answer of the POST尝试发送重定向 URL 作为 POST 的答案

@PostMapping("/carmodel")
public String aaa(){
    System.out.println("post request");

    return "/carmodelget";
    // or as json
    // return "{'url':'/carmodelget'}";

and then do a然后做一个

window.location.href = newUrl

on the client side.在客户端。

Making a redirect with parameters is quite simple:使用参数进行重定向非常简单:

@Controller
public class NController {
    @GetMapping("/carmodelget")
    public String tester(Model model, String param){
        System.out.println("get request");

        model.addAttribute("name", param);
        return "carmodel";
    }

    @PostMapping("/carmodel")
    public String aaa(){
        System.out.println("post request");
        String param = "test";
        return "redirect:" + "/carmodelget?param=" + param;
    }
}

add some lines in springmvc web.xml在 springmvc web.xml 中添加一些行

<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>  
    <filter-name>HiddenHttpMethodFilter</filter-name>  
    <servlet-name>springmvc</servlet-name>  
</filter-mapping> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM