ajsie
ajsie

Reputation: 79696

How to redirect a website from the server on an Ajax request

Is it possible to redirect a web site from the server on an ajax request?

Eg.

  1. The website (www.myapp.com) makes an ajax request to the server.
  2. The server redirects to the same URL with a hash eg. www.myapp.com#page=main.
  3. Website is redirected to www.myapp.com#page=main.

I need to redirect it to the same url but with a hash.

Using window.location.href = http://www.myapp.com#page=main won't work since it will just update the location with the hash.

Upvotes: 2

Views: 207

Answers (2)

genesis
genesis

Reputation: 50976

Aeah, it is. You can use meta or javascript redirect. Just append following code into an ajax reponse & make sure that reponse is placed into actual page

 <script>document.location.href='newpage.php'</script>

or to refresh, return

 <script>document.location.reload(1);</script>

Upvotes: 2

SLaks
SLaks

Reputation: 887469

No.
An AJAX request just returns data to the client; the browser does not act on it at all.

Instead, you need to make the server tell your client code to do a redirect in its response, then write client code to check whether the server asked it to redirect and navigate to the new location.

Upvotes: 2

Related Questions