首页 > 代码库 > 用ajax实现评论刷新

用ajax实现评论刷新

前台代码:

<script src="http://www.mamicode.com/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="http://www.mamicode.com/小事忠心.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="http://www.mamicode.com/提交" />
</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实现评论刷新