一个AJAX问题

Posted on 2008-04-14 11:29  BingLiang Sha  阅读(226)  评论(0编辑  收藏  举报

最近在AJAX实战书本中找到如下代码:
<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript">      
    var req=null;
    var console=null;
    var READY_STATE_UNINITIALIZED=0;
    var READY_STATE_LOADING=1;
    var READY_STATE_LOADED=2;
    var READY_STATE_INTERACTIVE=3;
    var READY_STATE_COMPLETE=4;

    function getXmlHttpRequest()
    {
        var xRequest=null;
        if(window.XMLHttpRequest)
        {
            xRequest=new XMLHttpRequest();
         }
         else if(window.ActiveXObject)
         {
            xRequest=new ActiveXObject("Microsoft.XMLHTTP");
         }
         return xRequest; 
    }
   
    function sendRequest(url,params,HttpMethod)
    {
        if(!HttpMethod)
        {  
            //这里改成POST
            HttpMethod="GET";
        }
        req=getXmlHttpRequest();
        if(req)
        {
            req.onreadystatechange=onReadyStateChange;
            req.open(HttpMethod,url,true);
            req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            req.send(params);
        }
    }
 

    function onReadyStateChange()
    {
        var ready=req.readyState;
        var date=null;
        if(ready == READY_STATE_COMPLETE)
        {
            data=req.responseText;
        }
        else
        {
            data="loading....["+ready+"]"; 
        }
        toConsole(data);
    }
    function toConsole(data)
    {
      if (console!=null)
      {
        var newline=document.createElement("div");
        console.appendChild(newline);
        var txt=document.createTextNode(data);
        newline.appendChild(txt);
      }
    }
   
    window.onload=function()
    {
        console=document.getElementById('console');
        sendRequest("data.txt");
    }
</script>
</head>
<body>
<div id='console'></div>
</body>
<html>
把请求的方法改成POST后,死活通不过。改回GET后就可以了。真想不明白,谁可以指教一二??