62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" language="javascript">
var xmlHttpRequest;
function createXMLHttpRequest() {
try {
// Firefox, Opera 8.0+, Safari
xmlHttpRequest = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
}
//发送请求函数
function sendRequestPost(url, param) {
createXMLHttpRequest();
xmlHttpRequest.open("GET", url + "?" + param, true);
xmlHttpRequest.onreadystatechange = processResponse;
}
//处理返回信息函数
function processResponse() {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
var res = xmlHttpRequest.responseText;
window.alert(res);
} else {
window.alert("请求页面异常");
}
}
}
function test() {
var comment = document.getElementById("tbCommentContent").value;
if (comment == "") {
alert("不能为空");
return false;
}
else {
var url = "TestAspDotNet/Main.aspx";
var param = "comment=" + comment;
sendRequestPost(url, param);
}
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tbCommentContent" runat="server" Height="188px"
TextMode="MultiLine" Width="334px"></asp:TextBox>
</div>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="test()" onclick="btnSubmit_Click"
/>
</div>
</form>
</body>
</html>
protected void btnSubmit_Click(object sender, EventArgs e)
{
object o = Request.QueryString["comment"];
//o 为null,不知道为什么 }
function processResponse() {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
var res = xmlHttpRequest.responseText;
window.alert(res);
} else {
//window.alert("请求页面异常");
window.alert(xmlHttpRequest.status);//弹出的提示为404;
}
}
}
function sendRequestPost(url, param) {
createXMLHttpRequest();
xmlHttpRequest.open("GET", url + "?" + param, true);
xmlHttpRequest.onreadystatechange = processResponse;
xmlHttpRequest.send(null);//添加这句 你要提交到服务器啊
}
protected void Page_Load(object sender, EventArgs e)
{
object o = Request.QueryString["comment"];
}