$.ajax 无法处理 php 脚本中的错误

发布于 2024-10-08 16:06:52 字数 790 浏览 0 评论 0原文

我有这些代码:

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

我的'test.php'是:

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

所以它有解析器错误,但ajax立即返回“True”,但如果第二行有注释,它会等待3秒。并返回“false”。有什么想法吗? 我已经更改代码 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
?>

所以现在 ajax 提醒我有关致命错误,但解析器错误没有捕获。有人知道这个问题的好解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

束缚m 2024-10-15 16:06:52

按设计工作。

如果存在解析器错误,PHP 脚本将失败并立即终止。然而,在 ajax 端,这仍然是成功的,因为 PHP 无论如何都会发送 200 OK 标头。

(对于致命错误,PHP 将发送 500 标头 仅当 error_reportingdisplay_errors 关闭时。)

如果没有解析器错误,脚本将休眠 8 秒,达到 3 秒超时,导致 Ajax 错误。

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.

尐籹人 2024-10-15 16:06:52

这是正常行为:在第一种情况下,您的 php 脚本返回 php 解析错误,并且 ajax 得到该错误,
所以你的ajax没问题。

接下来的情况是,您的 php 脚本运行时间超过了 ajax 的等待时间,因此您的 ajax 会弹出错误。

干杯阿曼。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文