0

I'm trying to redirect a user after a form submission with AJAX. I tried using window.location.assign, window.location.href and both didn't work.

When this function is triggered i receive the following text in Google Chrome console (This line refer to console.log(response), response is the url i get back from funcaoCla.php):

../usuario/cla?claatualizado

My JS function:

function funcaoCla(){
  var nomeCla = $("#nomeDoCla").val();
  var tagCla = $("#tagDoCla").val();
  var corDoCla = $("#corDoCla").val();
  if (verificaCla()){
    $.ajax({
      async: false,
      url: '../modulos/funcaoCla.php',
      type: 'post',
      data: {'cadastrar_cla':'cadastrar_cla', 'nomeCla' : nomeCla, 'tagCla' : tagCla, 'corCla' : corDoCla},
      success: function(response){
        console.log(response)
        window.location.assign(response)
      },
      error: function(response){
          console.log("ERRO")
          console.log(response)
          alert("Algo pode ter dado errado, atualize a pagina e tente novamente!")
      }
    })
  }else {
    return false;
  }
}
  • 2
    Perhaps you could give the smallest reproducable case so that we can run it ourselves? The only runnable part of your code snippet is `window.location.assign(response)` and that works from my side. – PaulBGD Jun 19 '17 at 22:11
  • Maybe you should try window.location.replace(response) – Adrianopolis Jun 19 '17 at 22:12
  • or `window.location.href = response` , but that depends on what `response` is, is it an URI ? – john Smith Jun 19 '17 at 22:13
  • Most likely you have a form that is refreshing the page before the ajax success runs. – Kevin B Jun 19 '17 at 22:14
  • Possible duplicate of [How to redirect to another webpage in JavaScript/jQuery?](https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage-in-javascript-jquery) – csklrnd Jun 19 '17 at 22:22
  • @Adrianopolis Already tried it. –  Jun 19 '17 at 22:33
  • @johnSmith My response: ../usuario/cla?claatualizado –  Jun 19 '17 at 22:34
  • @KevinB My form have a return, so just reload after some return from Ajax, i don't think is this but i'm not 100% sure. Have some way i can test this? –  Jun 19 '17 at 22:36
  • 1
    @JoãoZanetti but you only return if not doing ajax. you shoudl always return false if you don't want the form to default submit. – Kevin B Jun 19 '17 at 22:54
  • @KevinB I learned it in the hard way. I put a return false at the end of if and it worked. –  Jun 19 '17 at 22:58
  • there is no `../` in URI, thats why it doesnt work i think – john Smith Jun 20 '17 at 08:30

0 Answers0