I saw in another post ( jQuery Ajax PHP redirecting to another page ) that when using ajax to redirect to a PHP page we need to set an event like so :
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
success: function(r)
{
window.location = 'new.php';//window.location.href = 'new.php';
//$("#div").html(r);
},
});
However it's not clear to me :
Thanks!
AJAX is not for redirect. You can redirect only with this code:
window.location.href = 'new.php';
AJAX is to make requests to the server by asynchronous mode. The answers to your questions:
window.location.href
you can redirect What this code does is that it calls ajax.php, and when this call succeeds (which is always in this case, unless ajax.php has syntax error or sends error header), it executes the success function. Success function does the redirect to new.php.
If you just want to do a redirect in javascript, all you have to do is:
window.location = 'http://somewhere';
you probably don't need all this code you posted above.
let me explain you clearly
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
success: function(r)
{
window.location = 'new.php';//window.location.href = 'new.php';
//$("#div").html(r);
},
});
$.ajax this function actually makes ajax request to files ..
your questions
answer #1
this line means url: ajax.php ... where actually you want to make ajax request in this case code making ajax request to file ajax.php
answer #2
no this is totally separate from redirecting ..
answer #3
no if you change ajax.php with abc.html then it will not redirect..after changing it will make ajax request to abc.html now
and for redirecting ... use simple code
window.location.href = 'filename'; // replace filename with any file like new.php
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.