求助:通过JSON发送PHP错误

我正在调试一个页面,该页面通过ajax发送带有附件的电子邮件,但是在PHP中收到了“解析错误”,因为该错误未通过JSON发送回去。

请问有什么方法可以确切地看到即将发生的PHP错误?如果此表单不是使用Ajax提交的,我希望在js警报中看到的内容与在屏幕上看到的内容相同。

Use HTTP status codes. Simple and clean.

On the client side: You can handle the HTTP states with the success and failure callbacks of the common JavaScript frameworks.

On the server side: Use exceptions and handle them with a catch block. In the catch block set the HTTP status code and echo the exception message. Or use the less fancy set_error_handler() function and set the status code and error message in the callback function.

Fatal errors usually can't be handled *.

Try improving your code and just don't make parse errors or other fatal errors. Handling warnings and notices is easy: http://php.net/manual/en/function.set-error-handler.php

*: It's possible but it takes a lot of effort.

You'd use exception-handling, look here: http://www.php.net/manual/en/language.exceptions.php

function json_error($msg) {
    header('HTTP/1.1 500 ' . $msg);
    die(json_encode(array('error' => $msg)));
}

json_error('something bad happened');

This will trigger the error: ajax callback on the JS side.

echo json_encode(array('anything' => 'really', 'any key' => 'any value'));

Will trigger the success: callback.