javascript - Ajax POST 永远不会被发送?

标签 javascript php jquery ajax post

我的 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/

相关文章:

javascript - javascript 对象是否可以具有常量值?

javascript - 如何将 csrf_token 传递给 django 中的 javascript 文件?

php - 如何防止我的 WordPress 网站中的评论部分横跨全屏?

php - php Singleton 类实例会在多个 session 中保留吗?

php - 在 PHP 中使用 foreach 循环为每个结果分配变量

jquery - 带淡入淡出的简单背景 slider

javascript - 使用 javascript 将 HTML 页面与 SQL 服务器连接

jquery - jqChart日期标签只显示时间

javascript - 错误: SecurityError: DOM Exception 18 Tizen

c# - 更改 DropDownList 项取决于另一个 DropDownList(OnChange 事件)ASP.NET 网页中的值