xmlHttpRequest readyState卡在1

时间:2015-05-27 18:43:24

标签: javascript html asp.net ajax

我有一个简单的ajax代码(我试图在更新我的网站之前检查xml文件是否已更改)但由于某种原因,readystate卡在1并且onReadyStateChanged事件处理程序不会触发。

这是我的javascript

 window.setInterval(updateCount, 1000);
    var xmlhttp = init();

    function updateCount() {
        i = document.getElementById("updater")
        if (i.value != "") {
            if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
                xmlhttp.onreadystatechange = update_chacker;
                xmlhttp.open("GET", i.value, true);
                xmlhttp.send;
            }
        }
    }

    function init() {
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                return new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
        }

    function update_chacker() {

        var txt, x, xx, i;
        console.log("readystate = " + xmlhttp.readyState);
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            xx = document.getElementById("MessagesBulletedList");
            x = xmlhttp.responseXML.documentElement.getElementsByTagName("Message");
            if (x.length > xx.getElementByTagName('li').length) {
                var clickButton = document.getElementById("updater");
                clickButton.click();
            }
        }
    }

HTML(不太重要,但如果您想了解这里的参考资料)

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" width="300px" UpdateMode="Always" OnLoad="UpdatePanel1_Load" ViewStateMode="Enabled">
        <ContentTemplate>
            <asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>

            <br />
            <asp:Label ID="chatterLbl" runat="server" />
            <br />
            <div id="ChatText" style="width: 640px; height: 240px; overflow: auto;">
                <asp:BulletedList ID="MessagesBulletedList" runat="server" />
            </div>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="SendButton" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="updater" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>


    <div hidden="hidden"> <asp:Button ID="updater" runat="server" Text="" /> </div>
    <br />
    <asp:UpdatePanel ID="UpdatePanelTextBox" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:TextBox ID="NewMessageTextBox" Columns="50" runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button ID="SendButton" Text="Send" runat="server" OnClick="SendButton_Click" />

1 个答案:

答案 0 :(得分:4)

您永远不会致电发送

xmlhttp.send;

需要

xmlhttp.send();