我有一个使用PHP后端的遗留angular.js应用程序。如果PHP代码中存在错误,angular.js会显示一个无用的错误,如下所示:
angular.js:14199 SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at fromJson (angular.js:1345)
at defaultHttpResponseTransform (angular.js:10878)
at angular.js:10969
at forEach (angular.js:325)
at transformData (angular.js:10968)
at transformResponse (angular.js:11828)
at processQueue (angular.js:16696)
at angular.js:16712
at Scope.$eval (angular.js:17994)如果我查看开发人员控制台>网络>响应,我可以发现问题是PHP后端只是回显HTML而不是JSON,即类似于:
<br />
<b>Warning</b>: Declaration of Reminder::save($json) should be compatible with CRMTable::save($json, $customFilter = NULL, $addLogEntry = true) in <b>C:\depot\web\qms2\qms\php-api\crm.php</b> on line <b>710</b><br />
<br />有没有办法“捕捉”这些解析错误,以便我能更好地处理它们?使用try/catch包装HTTP POST调用无济于事:
try {
return $http.post(API_URL + '/login', xsrf, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}});
}
catch (e) {
console.log("HTTP error: ", e);
}发布于 2021-03-30 05:02:03
您可以执行以下操作:
set_error_handler("warning_handler", E_WARNING);
/// Run your code that throws the warning
// Wherever this is running Reminder::save($json);
restore_error_handler();
function warning_handler($errno, $errstr) {
// do something like log to a real place?
}https://stackoverflow.com/questions/66861536
复制相似问题