Why that header("Refresh:0"); does not work when ajax is running

HTML

 <form class="login" method="post" action="login.php"> Телефон <input type="text" name="phone" /> Пароль <input type="password" name="password" /> <button type="submit" name="submit">Войти</button> </form> 

Js

 $(document).ready(function(){ $('.login button').click(function(e){ e.preventDefault(); $.ajax({ type: 'POST', url: 'login.php', data: $('.login').serialize(), success: function(data) { notice() $('notice').html('<div class="notice">'+data+'</div>'); } }); }); }); 
  • one
    And what do you think this header should do when an AJAX request? - Dmitriy Simushev
  • @DmitriySimushev I need so that if the input is successful then refresh the page - KYRAN
  • one
    He does not have to update the page, javascript'vskiy location.reload() to help you. - Alex Krass
  • @AlexKrass I am a beginner how can I help please - KYRAN September

1 answer 1

You are trying to refresh the AJAX request page. This request is not the same page that you see. To refresh the page in your code, you need AJAX to return some value by which JS can unambiguously determine what needs to be done.

For example, instead of

 header("Refresh:0"); exit(); 

We form the following answer:

 exit('Reload'); //Reload можно заменить на что угодно 

and check in js:

  success: function(data) { if(data === 'Reload') //тот самый Reload, который можно заменить на что угодно location.reload(); //перезагружаем страницу через JS else { notice() $('notice').html('<div class="notice">'+data+'</div>'); } } 
  • Updated. There you can just add else - A1essandro
  • Thank you, it works! And you can ask me to have such a yes exit("<meta http-equiv='refresh' content='0; url= /'>"); and I changed something with a pat and stopped working why you know? - KYRAN September
  • Unfortunately, I worked with this tag only when I just tested it. I think if you dig, you can find the answer by downloading meta http-equiv refresh ajax - A1essandro
  • Thank you for helping! - KYRAN September