0

I am in a specific page and I have a function on Javascript:

function goToAnotherpage(){
    alert('goo');
    $.ajax({  
        type : 'POST',  
        url : "/firstpage",  

    });
}

In Controller I enter inside the method firstpage

@RequestMapping(value="/firstpage", method = RequestMethod.POST)
public ModelAndView goToAnotherPage() throws ServiceException {
    logger.debug("IT ENTER HERE);
    return  new ModelAndView("/secondpage");
}

I cannot enter in the second page. What is the problem?

Goldbones
  • 1,288
  • 1
  • 17
  • 42

1 Answers1

1

You can return the address and redirect in the javascript, something like this:

function goToAnotherpage(){
    alert('goo');
    $.ajax({  
        type : 'POST',  
        url : "/firstpage",  

    }).success(function(data){
        window.location.href = "http://mypage" + data; // http://mypage/secondPage
    });
}

Spring:

@RequestMapping(value="/firstpage", method = RequestMethod.POST)
public String goToAnotherPage() throws ServiceException {
    logger.debug("IT ENTERED HERE");
    return "/secondpage";
}
Carlos López Marí
  • 1,084
  • 1
  • 12
  • 30
BrTkCa
  • 4,419
  • 3
  • 20
  • 43
  • but, it must be automatically handled by spring .. i think that's what he was asking. – Atul Sharma Oct 18 '16 at 13:16
  • 1
    Spring as service not has the power to redirect some page. OP call the service and try return ModelAndView. Ajax request doesn't understand what's ModelAndView and he doesn't do nothing with the return. – BrTkCa Oct 18 '16 at 13:20
  • window.location.href refers to the current page? – Goldbones Oct 18 '16 at 13:30
  • Yes. To another page it's used: `window.open(url, '_blank');` – BrTkCa Oct 18 '16 at 13:33
  • The spring service received the request? And the ajax success received the response? – BrTkCa Oct 18 '16 at 13:43
  • I had success. I was doing wrong things. You have to put localhost:8080. How can I put this dinamically? – Goldbones Oct 18 '16 at 14:10
  • url can be: `document.location.host + ':' + document.location.port + data;` see that: http://stackoverflow.com/questions/2300771/jquery-domain-get-url – BrTkCa Oct 18 '16 at 15:06