简体   繁体   English

通过ajax发布请求似乎无法正常工作

[英]Post request through ajax doesn't appear to be working properly

I have form that makes a request via post managed by ajax. 我有通过ajax管理的帖子发出请求的表单。 However, it does not appear to be working at all. 但是,它似乎根本不起作用。 I know the php file works as I've tried it without using ajax in the middle. 我知道php文件可以工作,因为我已经尝试过,而不是在中间使用ajax。

This is my ajax request: 这是我的ajax请求:

function checkOut(params) {
    var urlString = params;
    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("test").innerHTML=xmlhttp.responseText;
        }
       //Setup for post submission  
       xmlhttp.open("POST","mockcheckout.php",true);
       xmlhttp.setRequestHeader("Content-length", params.length);
       xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

       xmlhttp.send(urlString); 

    }

} 

Any help is appreciated. 任何帮助表示赞赏。

Looks like you have a problem with code blocks. 看起来你的代码块有问题。 .open , .send should be outside of onreadystatechange handler scope. .open.send应该在onreadystatechange处理程序范围之外。

function checkOut(params) {
    var urlString = params;
    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("test").innerHTML=xmlhttp.responseText;
        }
    }
    //Setup for post submission  
    xmlhttp.open("POST","mockcheckout.php",true);  
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
    xmlhttp.send(urlString);     
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM