Ajax--readyState一直為0


readyState 一直為0表示對象已建立,連接未建立的狀態

我寫的代碼:

    function LoadXMLDoc(url,scope,orderID){
        if(window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }else if(window.ActiveXObject){
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        alert(xmlhttp);
        if(xmlhttp!=null){
            xmlhttp.onreadystatechange = changeState();
            xmlhttp.open("post",url,true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send("scope="+scope+"&orderID="+orderID);
        }
    }

    function changeState(){
        alert("q");
        alert('state:'+xmlhttp.readyState);
        alert('status:'+xmlhttp.status);
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            alert("Q");
            window.location.reload();
        }
    }

后來終於解決了,是因為這句:xmlhttp.onreadystatechange = changeState();這樣寫的話xmlhttp.onreadystatechange為將changeState的返回值賦給xmlhttp.onreadystatechange;而xmlhttp.onreadystatechange為當狀態改變時調用函數changeState,所以應該這樣寫:xmlhttp.onreadystatechange=changgeState,如果有參數的話這樣寫:xmlhttp.onreadystatechange = functiong (){changeState(a,b);};


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM