Javascript 空XmlHttp responsetext(仅限Google Chrome)

Javascript 空XmlHttp responsetext(仅限Google Chrome),javascript,xmlhttprequest,Javascript,Xmlhttprequest,我在Google Chrome中的聊天脚本有问题。 有时,在重新加载页面之前,responsetext是空的,但有时它工作正常。它每秒打开一个xmlhttp连接,如果第一个连接正常,那么后面的连接也正常。 在Firefox中,它总是好的 var url = "text.php"; xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange = myfunc; xmlHttp.send(null); function myfunc() {

我在Google Chrome中的聊天脚本有问题。 有时,在重新加载页面之前,responsetext是空的,但有时它工作正常。它每秒打开一个xmlhttp连接,如果第一个连接正常,那么后面的连接也正常。 在Firefox中,它总是好的

var url = "text.php";
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = myfunc;
xmlHttp.send(null);

function myfunc()
{
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    {
        var msg = xmlHttp.responseText;
        alert(msg);
    }
}
试试这个

var xmlHttp;
xmlHttp=new XMLHttpRequest();
var url = "text.php";
xmlHttp.onreadystatechange = function()
{
    if (xmlHttp.readyState==4 && xmlHttp.status==200)
    {
        var msg = xmlHttp.responseText;
        alert(msg);
    }
}
xmlHttp.open("GET", url, true);
xmlHttp.send(null);

您能否发布更多详细信息,例如
myfunc
的内容?您可以编辑您的问题以执行此操作。已编辑。myfunc()就在那里。我会尝试一下,然后很快回复。它几乎总是有效的,但有时不行。但它似乎更经常地起作用。按下“Shift+F5”后,它有时会很好。对于IE7和更高版本的浏览器,它肯定会工作。它什么时候不工作?这是因为上面的脚本在网页加载时工作,所以你必须刷新它才能工作。如果您希望它在按钮上工作,请单击将上述代码放入函数中,并在onclick event中调用该函数,但我不理解为什么它有时工作,有时不工作。