我有一个简单的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(不太重要,但如果您想了解此处的参考文献,则为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" />
最佳答案
你永远不会打电话给发送
xmlhttp.send;
需要是
xmlhttp.send();
关于javascript - xmlHttpRequest readyState卡在1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30490651/