我的 ajax POST 遇到问题,由于某种原因,该 POST 从未发出!难道我一辈子都无法解决这个问题吗?
是的,所以我使用 Firefox 中的网络调试工具来检查 POST 请求,但 POST 请求从未被发出..
该函数肯定也会被调用,因为我在运行的函数的开头添加了一个警报 alert("start")
。
AJAX
<script>
function updateContentNow(pid2, status2) {
var mypostrequest = new ajaxRequest();
mypostrequest.onreadystatechange = function() {
if (mypostrequest.readyState == 4) {
if (mypostrequest.status == 200 || window.location.href.indexOf("http") == -1) {
document.getElementById("livestats").innerHTML = mypostrequest.responseText;
} else {
alert("An error has occured making the request");
}
}
}
var parameters = "cid=clientid&pid=6&statusinfo=approve";
mypostrequest.open("POST", "http://mydomain.com.au/content-approval/ajax.php", true);
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
mypostrequest.send(parameters);
}
</script>
更新工作:谢谢大家..
<script>
function updateContentNow(pid2,status2)
{
var mypostrequest=new XMLHttpRequest()
mypostrequest.onreadystatechange=function(){
if (mypostrequest.readyState==4){
if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
document.getElementById("livestats").innerHTML=mypostrequest.responseText;
}
else{
alert("An error has occured making the request");
}
}
}
var parameters="cid=<?=$clientID?>&pid="+pid2+"&statusinfo="+status2;
mypostrequest.open("POST", "http://mydomain.com.au/content-approval/ajax.php", true);
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
mypostrequest.send(parameters);
}
</script>
最佳答案
您是否使用了一些外部 Ajax 类,至少 ajaxRequest() 对象在纯 JavaScript 中不存在。尝试替换这一行
var mypostrequest = new ajaxRequest();
由此:
var mypostrequest=new XMLHttpRequest();
然后甚至调用你的方法
updateContentNow("","");
至少发出 POST 请求,正如您可以通过 Firebug 轻松看到的那样。
关于javascript - Ajax POST 永远不会被发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21517627/