javascript ajax asp.net用get传参后台获取不到值

hhy1035402832 2011-01-10 10:50:34

<%@ 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,请各位大虾帮帮小弟
...全文
350 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
TimZhuFaith 2011-01-10
  • 打赏
  • 举报
回复
Page_Load里面返回你的requeststring[Quote=引用 4 楼 hhy1035402832 的回复:]
改了,还是不行。我把代码修改了一下,如下

HTML code

function processResponse() {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
var res = xmlHttp……
[/Quote]
hhy1035402832 2011-01-10
  • 打赏
  • 举报
回复
改了,还是不行。我把代码修改了一下,如下

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;
}
}
}


window.alert(xmlHttpRequest.status);//弹出的提示为404,也就是说TestAspDotNet/Main.aspx这个URL找不到?但是我Main.aspx这个页面是在TestAspDotNet项目里面啊,如果去掉,window.alert(xmlHttpRequest.status);弹出框里面是我页面的源码,我郁闷了,各位大虾给点力啊!!
telankes2000 2011-01-10
  • 打赏
  • 举报
回复

function sendRequestPost(url, param) {
createXMLHttpRequest();
xmlHttpRequest.open("GET", url + "?" + param, true);
xmlHttpRequest.onreadystatechange = processResponse;
xmlHttpRequest.send(null);//添加这句 你要提交到服务器啊
}

酷儿 2011-01-10
  • 打赏
  • 举报
回复
你会不会用JS传参呢 呵呵
hhy1035402832 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]
var xmlHttp=null;
function createXMLHttpRequest()
{
if(xmlHttp == null){
if(window.XMLHttpRequest) {
//Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
}else if(window.Active……
[/Quote]
ok了,弄好了,获取到了,xmlHttp.open("get", "TestAspDotNet/Main.aspx?val=" + val, true);
这个里面的URL错了,应该是xmlHttp.open("get", "Main.aspx?val=" + val, true);
感谢6楼的大虾,以及各位热心的大虾,但我还是不太懂Main.aspx在TestAspDotNet这个项目下面为什么URL只要Main.aspx就行了
hhy1035402832 2011-01-10
  • 打赏
  • 举报
回复
xmlHttp.send(null);有的
子夜__ 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hhy1035402832 的回复:]

C# code

protected void Page_Load(object sender, EventArgs e)
{
object o = Request.QueryString["comment"];
}


还是不行,o还是为null。
[/Quote]
xmlHttp.send(null);
有么?
hhy1035402832 2011-01-10
  • 打赏
  • 举报
回复

protected void Page_Load(object sender, EventArgs e)
{
object o = Request.QueryString["comment"];
}

还是不行,o还是为null。
wuyq11 2011-01-10
  • 打赏
  • 举报
回复
var xmlHttp=null;
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();
}
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧