我有一个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问题。
我在Firebug中看到的错误:
GET http://www.website.com/redeemvoucher.php?order=123456 500 Internal Server Error 1.33s
我可以为这个JS函数或文档设置延迟吗?准备好了吗?再说一遍,我真的不确定发生了什么。
我认为您的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');
不要依赖w3schools.com上的例子——人们普遍认为它们不可靠,维护不善。相反,只需使用$.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