2014-09-26 31 views
0

我有一個AJAX腳本,在我的網站上放置了一個命令後,我正在使用它來執行某些數據庫魔術。如何修復/調試間歇性地返回500錯誤的AJAX腳本

這是我如何引用它我的訂單確認頁面:

<script> 
// I define my function 
    function voucherRedeem(str) { 
     if (window.XMLHttpRequest) { 
     // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
     } else { // code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
      document.getElementById("confirmation").innerHTML=xmlhttp.responseText; 
     } 
     } 
     xmlhttp.open("GET","redeemvoucher.php?order="+str,true); 
     xmlhttp.send(); 
    } 

    // This grabs the order number in the confirmation field. 
    var finisher = jQuery('div.col-main p a:first').text(); 
    // This executes the function. 
    voucherRedeem(finisher); 
    </script> 

我有情況下,該腳本實際工作,但我看到500其他時候,我看到500,也沒有結果。最終,我不希望有任何500.這是一個具有大量資源的登臺服務器,所以我沒有預見到網絡或CPU問題。

我看到螢火蟲的錯誤:

GET http://www.website.com/redeemvoucher.php?order=123456 500 Internal Server Error 1.33s 

我可以設置延遲這個js函數或者一個的document.ready?再次,不確定發生了什麼。

+0

你爲什麼標籤這個問題[jQuery的]如果你不打算使用它呢? http://api.jquery.com/category/ajax – Blazemonger 2014-09-26 17:59:33

+0

我將刪除標籤,我在文檔的末尾使用jQuery來選擇元素的.text。 – sparecycle 2014-09-26 18:00:41

+0

仍然沒有解釋爲什麼不使用'.get('redeemvoucher.php',{order:str},function(data){/ * success * /})''。 – Blazemonger 2014-09-26 18:01:41

回答

2

您在我的PHP中有錯誤。

抓住它嘗試使用這個(您redeemvoucher.php的頂部):

function myErrorHandler($errno, $errstr, $errfile, $errline) { 
    echo $errno."\n".$errstr."\n".$errfile."\n".$errline; 
} 

function myFatalErrorShutdownHandler() { 
    $last_error = error_get_last(); 
    if ($last_error['type']===E_ERROR) { 
     myErrorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); 
    } 
} 


set_error_handler('myErrorHandler'); 
register_shutdown_function('myFatalErrorShutdownHandler'); 
+0

我能夠尾巴-f我的error.log,它確實提到了一個錯誤。顯然我調用的PHP文件不應該使用這個:$ results = $ readConnection-> fetchAll($ voucherattachquery); 錯誤:http://stackoverflow.com/questions/12979510/pdo-error-sqlstatehy000-general-error-when-updating-database – sparecycle 2014-09-26 18:16:20

0

不要依賴於w3schools.com的例子 - 他們被廣泛認爲是不可靠的,poorly-保持。所以,應該使用$.get做艱苦的工作:

$.get('redeemvoucher.php',{'order':str}) 
    .done(function(data) { 
     /* code you put in here will run after the AJAX returns successfully */ 
    }).fail(function() { 
     /* code you put in here will run after the PHP script returns an error */ 
    }); 

http://api.jquery.com/category/ajax

+0

比較起來似乎很簡單!現在嘗試。 – sparecycle 2014-09-26 18:12:17