简体   繁体   中英

Spring MVC AngularJS - Post Redirect

I believe I am just missing something obvious.

I have a post that works but it gives me HTML as response. Normally that would be great if I was actually trying to load some more information on the current page. But I really want it to redirect.

The post in MainService.js

searchOpportunities : function(title, location) {
                                return $http
                                        .post(
                                                '/',
                                                $.param({
                                                    title : title,
                                                    location : location
                                                }),
                                                {
                                                    headers : {
                                                        'Content-Type' : 'application/x-www-form-urlencoded'
                                                    }
                                                })
                            }

The response

@RequestMapping(value = "/", method = RequestMethod.POST)
    public ModelAndView search_Post(@RequestParam(value = "title", required = true) String title, @RequestParam(value = "location", required = true) String location) {
        ModelAndView searchView = new ModelAndView("search");
        searchView.addObject("searchTitle", title);
        searchView.addObject("searchLocation", location);
        return searchView;
    }

To clarify:

I want the page to change to the searchView after the post is sent. Right now it's just sending HTML as response... but I want redirect with the right objects "searchTitle" and "searchLocation"

Because you're just rendering the searchView view after POST , not redirecting to somewhere. If you really want to Redirect , use redirect: prefix. Suppose you have a /search endpoint and gonna redirect to it after successful POST on / , then:

@RequestMapping(value = "/", method = RequestMethod.POST)
public String search_Post(@RequestParam(value = "title", required = true) String title, @RequestParam(value = "location", required = true) String location) {
    ...
    return "redirect:/search";
}

You could render your searchView in /search endpoint, if you want to. Read more about Redirect Views here .

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