简体   繁体   中英

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)

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

@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.

The problem is that I get both "post request" and "get request" printed to the console in Spring, but the redirect never actually happens. What am I doing wrong?

Spring boot gives me an error: HttpRequestMethodNotSupportedException: Request method 'GET' not supported

And the browser console gives me an error: net::ERR_ABORTED 405

You can not redirect within a ajax call.

Try to send the redirect URL back as answer of the 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

<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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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