2013-02-27 52 views
1

我在我的應用程序中使用了幾個jQuery AJAX調用,並且在那些AJAX調用(例如500內部服務器錯誤)期間有時會遇到錯誤。爲了深入瞭解錯誤,我登錄到服務器並檢查錯誤日誌,該日誌爲我提供了PHP錯誤。向php腳本報告AJAX錯誤

我正在尋找一種方式來獲取這些錯誤發生時的通知以及導致錯誤的PHP錯誤。至少有一種獲得通知的方式,而不必接收用戶的電子郵件,然後進入並檢查日誌。

我檢查了這個教程,但不知道如何將其整合到AJAX錯誤中:http://net.tutsplus.com/tutorials/php/quick-tip-email-error-logs-to-yourself-with-php/並且我不確定這是否會返回PHP錯誤或500內部服務器錯誤。

任何想法,現有的服務,或解決方案,非常感謝!這裏是我的AJAX調用之一,例如:

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

編輯:

這裏是更新AJAX調用,使一個AJAX請求到PHP錯誤通知...

$.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 
}); 

這很好,但是,只報告發生了內部服務器錯誤,而不是導致AJAX錯誤的實際PHP錯誤。這裏是PHP錯誤處理文件:

$error = $_POST['error']; 
$sysadmin_message = $error; 
$sysadmin_to = '[email protected]'; 
$sysadmin_subject = 'Error Notification'; 
$sysadmin_headers = "From: Site <[email protected]>\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); 

顯然,這將只發送了AJAX的錯誤報道,但我怎麼添加實際PHP錯誤的通知?

+0

display_errors在您的php.ini文件中設置爲「On」? – ContextSwitch 2013-02-28 03:21:39

+0

如果不是,您可能想將其設置爲「On」,然後在每個非Ajax頁面上設置錯誤報告級別,以便不會向用戶顯示錯誤 – ContextSwitch 2013-02-28 03:22:30

+0

我仍然認爲try/catch會是更好的解決方案雖然 – ContextSwitch 2013-02-28 03:23:15

回答

3

您將需要使誤差函數內的第二AJAX調用到PHP

報告從前端:

$.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 
}); 

還是在後端使用try/catch語句:

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

但是,如果我將它設置爲發送錯誤,它只是報告發生了內部服務器錯誤。我仍然需要檢查日誌文件中的實際PHP錯誤。無論如何,有AJAX錯誤的PHP錯誤? – 2013-02-27 18:23:58

+0

如果服務器正在返回一個HTTP 500,則不需要。您可以將php.ini文件更改爲displayErrors = On,並且會顯示實際錯誤。只有在開發環境中才能做到這一點,而不是生產的好主意。爲什麼不使用php的錯誤日誌? – ContextSwitch 2013-02-27 18:35:31

+0

對不起,重新閱讀你的問題。你也可以嘗試用try catch塊捕獲php中的致命錯誤,然後發送自己的通知。更新了我的答案 – ContextSwitch 2013-02-27 18:54:13