2014-09-26 65 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