简体   繁体   中英

why do we need to place the source name to redirect?

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 :

  1. why do we need to indicate the " url: "ajax.php","
  2. should the url entry contain the name of the current file from which we're redirecting ?
  3. if i'm redirecting from a file called abc.html ? I should just replace the ajax.php with abc.html ?

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:

  1. Is the url to make requests. You don't need it if your only purpose is redirect.
  2. No if you don't will use it.
  3. NO. Only with 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

  1. why do we need to indicate the " url: "ajax.php","
  2. should the url entry contain the name of the current file from which we're redirecting ?
  3. if i'm redirecting from a file called abc.html ? I should just replace the ajax.php with abc.html ?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM