用ajax实现评论刷新

前台代码:

<script src="jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function () {

$("#btn").click(function () {
var text = $("#txt").val();
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open("POST", "ajaxTest.ashx?txt=" + text, true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
$("#span").html(xmlhttp.responseText);
$("#txt").val("");
}
else {
alert("AJAX服务出现了错误");
$("#txt").val("");
}
}
}
xmlhttp.send();
});
});
</script>

<body>
<video src="小事忠心.mp4" controls></video><br />
<label>评论:</label><br />
<span id="span"></span><br />
<textarea id="txt" style="width:670px;height:90px" name="txt"></textarea><br />
<input type="button" id="btn" value="提交" />
</body>

后台代码:

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string pathFile = context.Server.MapPath("~/PL.txt");
string txt = File.ReadAllText(pathFile);
string retxt=context.Request["txt"];
string dt = DateTime.Now.ToString();
File.AppendAllText(pathFile, "\r\n" + dt + ":" + retxt + "<br/>");
txt = File.ReadAllText(pathFile);
context.Response.Write(txt);
}

用ajax实现评论刷新

上一篇:Tomcat 架构原理解析到架构设计借鉴


下一篇:linux mailbox模型