αƞjiβ
αƞjiβ

Reputation: 3246

Redirection using Spring Controller and jQuery AJAX

I am using jQuery AJAX call to the Spring controller and depending on the task result I am either loading new content in div or redirecting the page. But my redirection portion is not happening. I can see in STS console controller being called but in browser it never get loaded.

Can anyone tell me what I am doing wrong? Am I using AJAX and Spring controller in right way?

Here is my code snippet:

A) AJAX Call

$('#someBtn').click(function(event) {
    $.ajax({
      type: 'POST',
      url: '/someApp/someLoc.htm',
      data: $('#someForm').serialize(),
      success: function(response) {
        $('#some-div').empty();
        $('#some-div').html(response);
      }
});

B) Controller

@RequestMapping(value = "/someApp/someLoc.htm", method = RequestMethod.POST)
public ModelAndView someTask(@ModelAttribute("someInfo") SomeInfoDto someInfo, BindingResult result) {
    if(result.hasErrors()) {
        return new ModelAndView("/myAppViewFolder/sameForm");
    }
    if(doSomethingGood()) {
        return new ModelAndView("redirect:/someApp/otherController.htm");  
    } else {
        return new ModelAndView("/myAppViewFolder/errorPage");
    }
}

Upvotes: 0

Views: 4213

Answers (1)

Panther
Panther

Reputation: 3339

You code is not loading the page, as this is ajax and form is not getting submitted, so you response will come in back in js function only. It will update your form but not navigate the page. You need to redirect page in client side depending on response.

window.location.href 

Upvotes: 1

Related Questions