向php脚本报告AJAX错误

时间:2021-11-19 01:26:33

I use several jQuery AJAX calls in my application and sometimes encounter errors during those AJAX calls such as 500 internal server error. To get to the bottom of the error, I log into the server and check the error log which provides me with the PHP error.

我在应用程序中使用了几个jQuery AJAX调用,有时在这些AJAX调用中会遇到错误,比如500个内部服务器错误。为了找出错误的根源,我登录到服务器并检查错误日志,该日志为我提供了PHP错误。

I'm looking for a way to get notifications when these errors happen and what the PHP error was that caused the error. At least a way to get notified instead of having to receive an email from an user and then go in and check the logs.

我正在寻找当这些错误发生时获取通知的方法,以及导致错误的PHP错误是什么。至少有一种方法可以得到通知,而不必从用户那里接收电子邮件,然后进入并检查日志。

I checked out this tutorial but am not sure how to integrate this into the AJAX error: http://net.tutsplus.com/tutorials/php/quick-tip-email-error-logs-to-yourself-with-php/ and also I'm not sure if this will return the PHP error or just the 500 internal server error.

我查看了本教程,但不确定如何将其集成到AJAX错误中:http://net.tutsplus.com/tutorials/php/quick tip-email-error- login - login - log-php/中,我也不确定这是否会返回PHP错误或500个内部服务器错误。

Any ideas, existing services, or solutions are greatly appreciated! Here is one of my AJAX calls for example:

任何想法、现有的服务或解决方案,我们都非常感激!下面是我的一个AJAX调用:

$.ajax ({
        url: report,
        cache: false,
        success: function(html) {
            // continue with event
        },
        error: function(x, status, error) {
            // report error here
        },
        async:true
});

EDIT:

编辑:

Here is the updated AJAX call that makes an AJAX requests to the php error notification...

下面是更新后的AJAX调用,它向php错误通知发出AJAX请求……

$.ajax ({
        url: report,
        success: function(html) {
            // success
        },
        error: function(x, status, error) {
            $.ajax ({
                url: 'admin/error_notification.php',
                type: "POST",
                data:{
                    error: error
                }
            });
        },
        async:true
});

This works great, however, only reports that an internal server error occurred instead of the actual PHP error that caused the AJAX error. Here is the PHP error handling file:

但是,这很有用,它只报告发生了内部服务器错误,而不是导致AJAX错误的实际PHP错误。下面是PHP错误处理文件:

$error = $_POST['error'];
$sysadmin_message = $error;
$sysadmin_to = 'email@example.com';
$sysadmin_subject = 'Error Notification';
$sysadmin_headers = "From: Site <accounts@example.com>\r\n";
$sysadmin_headers .= "MIME-Version: 1.0\r\n";
$sysadmin_headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($sysadmin_to, $sysadmin_subject, $sysadmin_message, $sysadmin_headers);

Obviously this will only send the AJAX error reported, but how do I add the actually PHP error to the notification?

显然,这只会发送报告的AJAX错误,但是如何将实际的PHP错误添加到通知中呢?

1 个解决方案

#1


3  

You would need to make a second ajax call inside of the error function to report to php

您需要在error函数中进行第二次ajax调用,以向php报告

From the front end:

从前端:

$.ajax ({
        url: report,
        cache: false,
        success: function(html) {
            // continue with event
        },
        error: function(x, status, error) {
            // report error here
            $.ajax ({
                url: errorlogUrl,
                data:{
                    message:'ajax error from [report]'
                }
            });
        },
        async:true
});

Or use a try/catch statement on the backend:

或在后端使用try/catch语句:

try{
    // code that might throw an error
}
catch(Exception $e){
    // send notification based on the content of $e
}

#1


3  

You would need to make a second ajax call inside of the error function to report to php

您需要在error函数中进行第二次ajax调用,以向php报告

From the front end:

从前端:

$.ajax ({
        url: report,
        cache: false,
        success: function(html) {
            // continue with event
        },
        error: function(x, status, error) {
            // report error here
            $.ajax ({
                url: errorlogUrl,
                data:{
                    message:'ajax error from [report]'
                }
            });
        },
        async:true
});

Or use a try/catch statement on the backend:

或在后端使用try/catch语句:

try{
    // code that might throw an error
}
catch(Exception $e){
    // send notification based on the content of $e
}