0

I apologize for my bad english :)

I'm doing php file with ajax request. json response comes in the format of. but in some cases can be redirect. In this case I want the redirect of the page.

Could you please help me. Thank's.

Example PHP File :

<?php
$status = $_POST['status'];

if($status == 'a'){
    // return json response
}else{
   echo "<form action='http://www.url.com'>..</form><script type='text/javascript'>form.submit();</script>";
}
?>

Example JS File :

$.ajax({
  type: "POST",
  url: 'http://www.my_php_file.com'
});
Murat SAÇ
  • 396
  • 1
  • 3
  • 13

3 Answers3

0

You can issue the redirect in the usual way, by returning a 3xx response with a Location header. The browser will follow the redirect (it doesn't make the JavaScript code doing the ajax call handle it).

So for instance, if you want to issue a 301 ("permanent moved"):

header('Location: http://www.google.com',true,301);
exit;

(I didn't know the specifics of how to issue the 3xx in PHP, but found them in this answer).

Side note: I doubt www.google.com is going to work, I assume that was just an example.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Use window.location = "http://www.yoururl.com"; to issue a redirect via JS.

Basit
  • 1,830
  • 2
  • 31
  • 50
0

In your php file replace php redirection with javascript redirection like this:

<script type="text/javascript">
window.location.replace("http://www.google.com");
</script>
Neeraj Kumar
  • 506
  • 1
  • 8
  • 19