我正在从xml文件中读取文本,并将此文本插入Label中。我每隔5秒就从xml文件中读取文本,以防它已经改变。这意味着我每隔5秒将文本插入标签(无论它是否已更改)。 现在的问题是,当我第二次执行GET时,我的xml文件被删除,这导致500(内部服务器错误),因为该文件不再存在。
以下是创建xml文档的代码
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public string GetMessage() {
XmlTextReader reader = new XmlTextReader (Global.sAppPath + "/alt/importantMsg.xml");
string message = null;
while (reader.Read()) {
if (reader.IsStartElement ()) {
switch (reader.Name.ToString ()) {
case "Message":
message = reader.ReadString();
break;
}
}
}
return message;
}
以下是发布GET的代码
window.setInterval(function() {
msg;
$.ajax({
url: "isoServe.asmx/GetMessage",
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
msg = data.d;
},
error: function() {
alert("Error");
}
});
Ext.getCmp('attentionLabel').setText(msg);
Ext.getCmp('attentionLabel').show();
}, 5000);
下面是发出POST的代码,将消息保存到xml文件以及客户端代码
[WebMethod]
public void AddToXMLFile (string param)
{
XmlDocument doc = new XmlDocument();
DateTime today = DateTime.Today;
XmlNode rootNode = doc.CreateElement("Scant");
XmlNode dateNode = doc.CreateElement("Date");
XmlNode importantMsg = doc.CreateElement("Message");
XmlAttribute msgId = doc.CreateAttribute("id");
msgId.Value = "msgID";
dateNode.InnerText = today.ToString();
importantMsg.InnerText = param;
importantMsg.Attributes.Append(msgId);
rootNode.AppendChild(dateNode);
rootNode.AppendChild(importantMsg);
doc.AppendChild(rootNode);
doc.Save(Global.sAppPath + "/alt/importantMsg.xml");
}
客户端代码
function saveMsg(msg) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "isoServe.asmx/AddToXMLFile", true); //async
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send("param=" + msg);
}
任何人都可以看到为什么xml文件突然被删除,导致内部服务器错误?
firebug中显示的一条错误消息是:
Document element did not appear. file:///var/www/topix/alt/importantMsg.xml Line 1, position 1.","StackTrace":"