How to redirect to a page according to the response received from php in ajax?

How can I redirect to a page according to the response received in php

success: function(datos){
  if(datos=='1'){
    //url 
  }else{
    //url 
  }
}
 2
Author: Mariano, 2016-07-29

4 answers

So you could redirect from javascript:

 success: function(datos){

 if(datos=='1'){
   location.href ="http://www.pagina1.com";
   }
    else {
     location.href="http://www.pagina2.com";
   }
 }

Greetings..

 5
Author: J. Armando, 2016-07-29 17:44:14

In javascript you can use the window property.location to redirect to another page:

// funciona como una redirección HTTP
window.location.replace("http://sitioweb.com");

// funciona como si dieras clic en un enlace
window.location.href = "http://sitioweb.com";
 2
Author: Shaz, 2016-07-29 14:36:41

Depending on what you try, you can choose to use one of these:

windows.location.href = 'http://url.com';: assigning this value keeps the current entry in the browser history. So if you press the back button of the browser, it will return to the page that made the redirect. That is the page where you run the ajax.

windows.location.replace('http://url.com');: instead this method replaces the current entry of the history so when you do back you will return to the previous url to the page that made the redirect.

Depending on the use case, one or the other is used. That is to say that if doing back, it will re-launch the redirect automatically, it would be advisable to use replace to not block the user the back button.

 1
Author: rnrneverdies, 2016-07-29 18:07:52

For addressing a page according to the response of AJAX use this:

$.ajax({
        type: "POST",
        url: "url.php",
        data: {datos:"datos"},
        success: function (data) {
          //data es la respuesta del php
          if(data){
            window.location.replace("http://sitioweb.com");
          }

      }
});
 0
Author: Fen Dev, 2016-07-29 16:57:36