日期:2014-05-16  浏览次数:20730 次

Ajax载入HTML文档为什么换成POST 方式就不行呢 - Web 开发 / Ajax
下面的代码在Open方法里用Get方式载入可以,用Post方式就不行了,至少在Firefox中是这样的,在IE中好像有时可以,有时又不行,为什么呢
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax技术加载HTML文档</title>
<script type="text/javascript">
function Ajax(){                //通过这个函数来异步获取信息
    var xmlHttpReq;
    if (window.ActiveXObject){            //针对IE6
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest){        //针对除IE6 以外的浏览器
        xmlHttpReq = new XMLHttpRequest();//实例化一个XMLHttpRequest
        
    }
    //xmlHttpReq.setRequestHeader("Cache-Control","no-cache"); 
    if(xmlHttpReq!= null){        //如果对象实例化成功 
        //var url = "9-2.html?r="&r=" + Math.random();
        xmlHttpReq.open("POST","9-2.html?t="+ new Date().getTime());    //调用open()方法并采用异步方式
        xmlHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
        xmlHttpReq.onreadystatechange=RequestCallBack; //设置回调函数
    
        xmlHttpReq.send(null);    //因为使用get方式提交,所以可以使用null参调用
    }
    function RequestCallBack(){        //一旦readyState值改变,将会调用这个函数
        if(xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200){
        document.getElementById("target").innerHTML = xmlHttpReq.responseText;
        }
    }
}
</script>
</head>
<body>

<input type="button" value="Ajax载入" onclick="Ajax();" />
<div id="target"></div>
</body>
</html>



------解决方案--------------------
探讨
要POST到html的话,服务器端要允许.html后缀接受POST头