爲了瀏覽器兼容,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>