我的Ajax javascript代码不起作用

我的Ajax javascript代码不起作用,javascript,ajax,Javascript,Ajax,我的Ajax javascript代码无法工作。。。 代码如下: function getMessages() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatec

我的Ajax javascript代码无法工作。。。 代码如下:

function getMessages() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("box").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "process.php");
    xmlhttp.send();
}
我的php脚本只是在做一个测试:这是一个测试。 我不知道我做错了什么。Chrome javascript调试器说我有一个未预期的令牌

PHP代码:

    <?php if (!$_COOKIE["name"]) {
    echo "<form method='get' action = 'process.php'><input type = 'text' name = 'name'><input type = 'submit' value = 'Submit'></form>";
        } else {
     echo ' 
function getMessages(){var xmlhttp;
if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest (); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
        xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4&&xmlhttp.status==200)   {     document.getElementById("box").innerHTML=xmlhttp.responseText; } }
        xmlhttp.open("GET","process.php") ;
        xmlhttp.send(); }
        var is={ie:navigator.appName=="Microsoft InternetExplorer",java:navigator.javaEnabled(),ns:navigator.appName=="Netscape",ua:navigator.userAgent.toLowerCase(),version:parseFloat(navigator.appVersion.substr(21))||parseFloat(navigator.appVersion),win:navigator.platform=="Win32"
}
   is.mac=is.ua.indexOf("mac")>=0;
if(is.ua.indexOf("opera")>=0){is.ie=is.ns=false;is.opera=true;}
        if(is.ua.indexOf("agecko")>=0)
        {is.ie=is.ns=false;is.gecko=true;
}';
    echo "<textarea id=box rows=20 cols=20></textarea>
<form method='get' action = 'process.php'> 
<input type = 'text' name = 'message'>
<input type = 'submit' value = 'Submit'>
 </form>";
    } ?>

xmlhttp.readyStat应该是xmlhttp.readyState,也许?

readyStat应该是readyState

xmlhttp.send to xmlhttp.sendnull也可以防止IE漏洞

可能无法解决您的问题,但xmlhttp.readyStat不应该是xmlhttp.readyState结尾的e缺失吗?您应该添加您正在使用的PHP代码。谢谢,但这并不能解决错误编辑:删除了我添加的一些额外内容。应该与代码相同,只是格式更好。试试这个,而不是你当前的PHP代码:你有一个页面,你已经实现了这个,我们可以检查吗?环境有时会有所帮助。函数本身很好,正如JSFIDLE所示。Safari控制台给了我以下错误:ReferenceError:找不到变量:getMessages应该在第11行//编辑:登录后,我也会收到此错误SyntaxError:需要一个标识符,但找到的是“xmlhttp”,而不是第28行。好的,登录。我注意到你的代码没有关闭,所以整行代码(包括和里面的所有内容)都被视为Javascript。顺便说一句,我是一个Javascript初学者。我有一种奇怪的感觉,就是空格和新行把事情搞砸了。