In a page, test.php
, I simply activate error reporting, I deactivate the logging, and I call a function test()
, which does not exist. As expected, if I run the code I receive the error message:
(!) 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
Now, in another page, index.php
, I have only a button - named testButton
. If I press it, an ajax request to the page test.php
is performed. In index.php
I expected to see that:
- The error thrown in
test.php
is handled by theerror
callback of the ajax request; - The error is displayed on screen.
Unfortunately none of this happens. When I press the button:
- The
success
callback of the ajax request is called; - No error is displayed on screen.
Could you help me find the problem, or identify the bug?
Thank you.
Used system:
- PHP: 7.1.1
- Apache Version: 2.2.31
- Apache API Version: 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>