做一个留言程序时没反应,测试了下是因为readystate为undefined。后台也可以讲一下,网上搜来是众说纷纭
源码如下:
chat.jsfunction sendmsg(){
    var xmlHttp;
    if(window.XMLHttpRequest){
        xmlHttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        alert("\\你的浏览器不支持Ajax!");
    }
    xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readystate == 4){
            if(xmlHttp.status == 200){
            document.getElementById("123").innerHTML = xmlHttp.responseText;
            }
        }else{
            alert("错误! 进展:"+xmlHttp.status+";  状态:"+xmlHttp.readystate);
            }
    }
    var msg = document.getElementById("mytext").innerHTML;
    var enmsg = encodeURIComponent(msg);
    xmlHttp.open("POST","server.php?",true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencode");
    xmlHttp.send("enmsg="+ enmsg);
}后台的 server.php<?php
//header("Cache-Control:no-cache;must-revalidate");
$enmsg = file_get_contents("php://input");
echo "<li>".$enmsg["enmsg"]."</li>";
?>
请求好人帮帮忙

解决方案 »

  1.   


    xmlHttp.onreadystatechange=function()
      {
      if (xmlHttp.readyState==4 && xmlHttp.status==200)
        {
        document.getElementById("123").innerHTML = xmlHttp.responseText;
        }else{
                alert("错误! 进展:"+xmlHttp.status+";  状态:"+xmlHttp.readyState);
                }  } 
      

  2.   

    你是在自己的机子上试的吗?我记得好像在书上看到要是在自己机子上测试要加上xmlHttp.status == 0;要不就是xmlHttp.readyState==0
      

  3.   

    readyState
    第二个单词state S要大写
    没办法死规定
      

  4.   

    用 JQ 的ajax 不是挺方便么。。
      

  5.   

    xmlHttp.status ==200代表Okey,xmlHttp.readyState==4是接收完成 谢谢
      

  6.   

    //我给你提供一段jquery  ajax代码
    $.get('text.php',{id:'1'},function(data){
           //随便你要执行的js 代码 你说jquery难不难???
    });//就这样ajax
      

  7.   


    function sendmsg(){
        var xmlHttp;
        if(window.XMLHttpRequest){
            xmlHttp = new XMLHttpRequest();
        }
        else if(window.ActiveXObject){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else{
            alert("\\你的浏览器不支持Ajax!");
        }
        xmlHttp.onreadystatechange = function(){
            if(xmlHttp.readyState == 4){//重点在这readyState 的S要大写
                if(xmlHttp.status == 200){
                document.getElementById("123").innerHTML = xmlHttp.responseText;
                }
            }else{
                //这个地方已经取不到xmlHttp.status的值了
                //alert("错误! 进展:"+xmlHttp.status+";  状态:"+xmlHttp.readystate);
            }
        }
        var msg = document.getElementById("mytext").innerHTML;
        var enmsg = encodeURIComponent(msg);
        xmlHttp.open("POST","server.php?",true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencode");
        xmlHttp.send("enmsg="+ enmsg);
    }$enmsg = file_get_contents("php://input");
    parse_str($enmsg);
    echo "<li>".$enmsg."</li>";
      

  8.   

    谢谢 jquery我还没研究过,还处于学习阶段,你这么说我更有信心学了
    jquery我还没研究过,还处于学习阶段,还是不必了,因为给我我暂时看不懂
    接着就会研究
    很感谢
      

  9.   

    readyState我已经主意到了,parse_str()果然是必须的
    另外我也犯了个低级错误——var msg = document.getElementById("mytext").innerHTML;是一个textarea取文本要用value而不是innerHTML;
    谢谢你的纠正
      

  10.   


    再次代表党和国家感谢Ladies and乡亲们!!