在ajax请求回调中显示PHP错误



在页面test.php中,我只是激活错误报告,停用日志记录,然后调用一个不存在的函数test()。正如预期的那样,如果我运行代码,我会收到错误消息:

(!) Fatal error: Uncaught Error: Call to undefined function test() in [path-to]/test.php on line 7
(!) Error: Call to undefined function test() in [path-to]/test.php on line 7
Call Stack
# Time    Memory  Function    Location
1 0.1336  355848  {main}( )   .../test.php:0

现在,在另一个页面index.php中,我只有一个名为testButton的按钮。如果我按下它,就会执行对页面test.php的ajax请求。在index.php中,我期望看到:

  • test.php中抛出的错误由ajax请求的error回调处理
  • 错误显示在屏幕上

不幸的是,这些都没有发生。当我按下按钮时:

  • 调用ajax请求的success回调
  • 屏幕上不会显示任何错误

你能帮我找到问题或识别错误吗?

谢谢。


使用的系统:

  • PHP:7.1.1
  • Apache版本:2.2.31
  • Apache API版本:20051115
  • jQuery:3.3.1

test.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 0);
$data = test();

index.php:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<title>Test: Displaying Errors</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#testButton').click(function (event) {
$.ajax({
method: 'post',
dataType: 'html',
url: 'test.php',
data: {},
success: function (response, textStatus, jqXHR) {
$('#result').html('Successful test... Unfortunately :-)');
},
error: function (jqXHR, textStatus, errorThrown) {
/*
* When an HTTP error occurs, errorThrown receives the textual portion of
* the HTTP status, such as "Not Found" or "Internal Server Error". This
* portion of the HTTP status is also called "reason phrase".
*/
var message = errorThrown;
/*
* If a response text exists, then set it as message,
* instead of the textual portion of the HTTP status.
*/
if (jqXHR.responseText !== null && jqXHR.responseText !== 'undefined' && jqXHR.responseText !== '') {
message = jqXHR.responseText;
}
$('#result').html(message);
}
});
});
});
</script>
</head>
<body>
<h3>
Test: Displaying Errors
</h3>
<div id="result">
Hier comes the test result. Since an error is thrown, I expect it to appear hear.
</div>
<br/>
<form method="post" action="">
<button type="button" id="testButton" name="testButton">
Start the test
</button>
</form>
</body>
</html>
jQueryAJAX调用中的successerror函数只需检查请求的HTTP状态代码。当PHP产生错误时,它不会更改状态代码,因此jQuery会触发success函数。如果要使用jQuery错误,则必须更改状态代码。您可以通过捕获Error(PHP>=7(:来实现这一点
try {
$data = test();
}
catch (Error $e) {
http_response_code(500);
echo $e->getMessage();
}

或者,您可以将状态代码保持为200(默认(,并以JSON发送响应,包括您喜欢的成功和错误属性,以及您可能希望随请求返回的任何其他内容。

header('Content-type: application/json');
$response['success'] = true;
$response['error'] = null;
try {
$data = test();
}
catch (Error $e) {
$response['success'] = false;
$response['error'] = $e->getMessage();
}
echo json_encode($response);

并确保从ajax请求中删除dataType属性,以便jQuery自动确定类型头。

如果你的程序意外地抛出错误,这可能意味着你做错了什么。如果你期望它抛出错误,你需要抓住它们。这要么意味着将事物封装在try/catch块中,要么意味着set_exception_handler()set_error_handler()register_shutdown_function()的组合来全局实现这一点。

最新更新