1

I have these code:

<script>
$.ajaxSetup({
 timeout:3000,
 success: function(){alert('True!!!');},
 error:function (){
                    alert('false');
                }
});
$.ajax({
 url:'test.php',
});
</script>

My 'test.php' is:

<?
sleep(8);
$124415;
?>

So it`s have parser error, but ajax return "True" immediately, but if the second line have comment, it waiting for 3 sec. and return "false'. Any ideas ? I have change code test.php:

<? 
function shutdown() {
    header("HTTP/1.0 500 Internal Server Error");
ob_flush();
}
ob_start();
register_shutdown_function('shutdown');
spl_autoload_register('foo');// Fatal Error => Ajax false
//$112123123;//if turn on will be Parse Error => Ajax true
?>

So now ajax alert me about fatal error, but Parser Error is not catching. Is anybody know good solution for this problem?

Kirill
  • 43
  • 4

2 Answers2

6

Works as designed.

If there's a parser error, the PHP script will fail and terminate immediately. On the ajax end however, this is still a success because PHP sends a 200 OK header anyway.

(PHP will send a 500 header for fatal errors only if error_reporting and display_errors are turned off.)

If there's no parser error, the script will sleep for 8 seconds, reaching the 3 second timeout, resulting in an Ajax error.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • So my new test.php : error_reporting(0); echo ini_set('display_errors',0); sleep(8); – Kirill Dec 23 '10 at 13:32
  • @Kirill if you're on PHP > 5.2.4, this should throw a 500 – Pekka Dec 23 '10 at 13:33
  • i cant believe that ajax construction will not display error without changing PHP – Kirill Dec 23 '10 at 13:44
  • @Kirill a PHP error is not a HTTP error, that's the way it is... You could create a custom error handler to send a 500 header but sadly, even that won't work in case of a parse error. – Pekka Dec 23 '10 at 13:48
  • Ok, I run my script at PHP 5.3.3 with your error off solution, this still not working. – Kirill Dec 24 '10 at 09:16
0

It is normal behaviour: in the first case your php script returns php parse error and ajax gets that, so your ajax went OK.

Next case your php script runs longer than ajax can wait, so your ajax pops up the error.

Cheers Arman.

Arman
  • 4,566
  • 10
  • 45
  • 66