Ajax onreadystatechange函数无法运行

时间:2017-11-07 19:55:17

标签: javascript php ajax html-form

我试图用ajax解决表单提交,php运行,但表单被卡住了。此函数不运行:ajax.onreadystatechange = function()

btw状态为0,因为控制台说...所以我不知道这里的问题是什么。

我的代码:

<script>
            function _(id){ return document.getElementById(id); }
            function submitForm(){
                _("btnSubmit").disabled = true;
                var formdata = new FormData();
                formdata.append( "name", _("name").value );
                formdata.append( "email", _("email").value );
                formdata.append( "phone", _("phone").value );
                formdata.append( "officeAddress", _("officeAddress").value );
                formdata.append( "officeType", _("officeType").value );
                formdata.append( "message", _("message").value );
                var ajax = new XMLHttpRequest();
                ajax.open( "POST", "contact.php");
                console.log(ajax.status)
                ajax.onreadystatechange = function() {
                    if(ajax.readyState == 4 && ajax.status == 200) {
                        if(ajax.responseText == "success"){
                            _("btnSubmit").disabled = false;
                            $('#myAlert').show('fade');  
                        } else {
                            _("btnSubmit").disabled = false;
                        }
                    }
                }
                console.log(ajax.status)
                ajax.send ( formdata );
            }
    </script>

0 个答案:

没有答案