<%@ 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,不知道为什么 }上面是aspx和aspx.cs代码,不知道为什么,不管我在textbox填什么o都为null,请各位大虾帮帮小弟
function sendRequestPost(url, param) {
createXMLHttpRequest();
xmlHttpRequest.open("GET", url + "?" + param, true);
xmlHttpRequest.onreadystatechange = processResponse;
xmlHttpRequest.send(null);//添加这句 你要提交到服务器啊
}
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
var res = xmlHttpRequest.responseText;
window.alert(res);
} else {
//window.alert("请求页面异常");
window.alert(xmlHttpRequest.status);//弹出的提示为404;
}
}
}
window.alert(xmlHttpRequest.status);//弹出的提示为404,也就是说TestAspDotNet/Main.aspx这个URL找不到?但是我Main.aspx这个页面是在TestAspDotNet项目里面啊,如果去掉,window.alert(xmlHttpRequest.status);弹出框里面是我页面的源码,我郁闷了,各位大虾给点力啊!!
function createXMLHttpRequest()
{
if(xmlHttp == null){
if(window.XMLHttpRequest) {
//Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject) {
// IE浏览器
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
}
}
function openAjax()
{
if( xmlHttp == null)
{
createXMLHttpRequest();
if( xmlHttp == null)
{
//alert('出错');
return ;
}
}
xmlHttp.onreadystatechange=xmlHttpChange;
var val=document.getElementById("<%=txt.ClientID %>").value;
xmlHttp.open("get","Default.aspx?val="+val+"&date="+new Date().getTime(),true);
xmlHttp.send(null);
}
function xmlHttpChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById('resultDiv').innerHTML=xmlHttp.responseText;
}
}
}
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["val"]))
{
Response.Clear();
Response.Write("");
Response.End();
}
}
protected void Page_Load(object sender, EventArgs e)
{
object o = Request.QueryString["comment"];
}还是不行,o还是为null。
有么?
这个里面的URL错了,应该是xmlHttp.open("get", "Main.aspx?val=" + val, true);
感谢6楼的大虾,以及各位热心的大虾,但我还是不太懂Main.aspx在TestAspDotNet这个项目下面为什么URL只要Main.aspx就行了