創建xmlHttpRequest對象在FireFox中支持Ajax

    爲了瀏覽器兼容,xmlhttp請求時,如果用到post方法,一定要先使用xhttp.setRequestHeader方法設計其http請求的head,只要將'Content-type'顯式設置爲'application/x-www-form-urlencoded',post方法就會有效了,否則很有可能出現一些奇怪的請求錯誤,比如411 content-length錯誤。。。
    還有,post方法時,不可以xmlhttp.send(null);如果沒有發送應該用xmlhttp.send('');否則也會出錯。。。

例:

<script language="javascript">
function getXMLHttpRequest()    
{
        var xmlHttpRequest;
        if (window.XMLHttpRequest) //For general cases.
        {
            //alert("0");
            xmlHttpRequest = new XMLHttpRequest();
        }
        else //For IE.
        {
            if (window.ActiveXObject)
            {
                //alert("1");
                xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttpRequest;
    }

function Show1(str)
{
     xmlHttp=new getXMLHttpRequest();
     xmlHttp.open('post','ajax.asp?id='+str,false);
     xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;");      xmlHttp.send('');
     if(xmlHttp.status==200) document.getElementById("Info").innerHTML=xmlHttp.responseText;
}
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章