Ajax请求发布

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'
});

Try this.

url: "http://www.my_php_file.com",
success: function(data) {
      document.location.href='YouNewPage.php';
}

Return the html that you want to echo as JSON also:

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

And check redirect in the ajax response:

$.ajax({
   type: "POST",
   dataType: "json",
   url: 'http://www.my_php_file.com',
   success: function(data) {
      if(typeof data.redirect !== "undefined") {
         $("body").append(data.redirect);
      }
   }
});

Just two reminders, there will be no redirection if request fails( no fail callback) and I assume your casual JSON response doesn't have an attribute redirect.

You need to detect if the response data is valid JSON:

$.ajax({
  type: "POST",
  url: 'http://www.my_php_file.com',
  success: checkAJAX
});

function checkAJAX(data)
{
    var response = $.parseJSON(data);
    if(typeof response === "object")
    {
    }
    else
    {
        // If the AJAX response is not JSON, append the HTML to the document
        $('body').append(data);
    }
}

Use success function https://api.jquery.com/jQuery.ajax/

$.ajax({
  type: "POST",
  url: 'http://www.my_php_file.com'
 data: { status : statusVar },
success: function(response){
if (response.status == 'a'){
$( "#results" ).append( response);
}else{
window.location = 'http://www.url.com'
}
});
});