为了浏览器兼容,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>