谁能帮我写个Ajax Post提交数据的方法

骑猪看海 2009-12-29 05:06:18
其实在网上搜的到很多,但大部分只写了前台js代码:
//发送请求函数
function sendRequestPost(url,param){
createXMLHttpRequest();
xmlHttpRequest.open("POST",url,true);
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttpRequest.onreadystatechange = processResponse;
xmlHttpRequest.send(param);
}
var url = "Default2.aspx";
var param = "userName="+userName+"&psw="+psw;
sendRequestPost(url,param);

但是我不知道在后台也就是Default2.aspx页面怎么接收这些参数,网上找的代码都不全面。
现在我要做的是提交评论,有题标内容什么的,提交完后无刷新显示在当前页面的Repeater上(我们老大不让用.net自带的Ajax框架),get方式提交我会但内容一多就会丢失!所以想用Post方式,但不会。谁有完整代码给我看看,发我邮箱上也行:qmialo88@163.com 谢谢各位大虾了!
...全文
948 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
骑猪看海 2009-12-30
  • 打赏
  • 举报
回复
楼上面说的都很实用,可我想要的文章评论无刷新显示呢,谁给我个例子
gaoqingchun2007 2009-12-29
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["key"] == "btn1")
{
string name = Request.Form["name"];
Response.Clear();
Response.Write(GetClass(name));
Response.End();
}

}
bancxc 2009-12-29
  • 打赏
  • 举报
回复
request.form
Dream_Hunter_ 2009-12-29
  • 打赏
  • 举报
回复

<script type="text/javascript">
$(document).ready(function(){
$("#div1").ajaxStart(function(){
$("#div1").html("正在加载");
});

$("#btn1").click(function(){
$.post("jQuery.aspx",{key:'btn1',name:$("#txt").val()},function(msg){
$("#div1").html(msg);
});
});
</script>

<input type="button" id="btn1" value="btn1" />
<div id="div1"></div>



protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["key"] == "btn1")
{
string name = Request.Form["name"];
Response.Clear();
Response.Write(GetClass(name));
Response.End();
}

}

private string GetClass(string name)
{
SqlConnection con = new SqlConnection("server=.;database=stu;uid=sa;pwd=sa");
SqlDataAdapter sda = new SqlDataAdapter("select class from stu where name='" + name + "'", con);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
return ds.Tables[0].Rows[0]["class"].ToString();
}
else
{
return "此人无对应的班级。";
}
}
wuyq11 2009-12-29
  • 打赏
  • 举报
回复
xmlHttp.open("POST", "test.aspx",true);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)alert(xmlHttp.responseText);
else alert("发生错误!");
}
}
xmlHttp.send("");


if(Request.Form["a"]!=null){}
vavsoft 2009-12-29
  • 打赏
  • 举报
回复
用JQuery简单啊。

$.ajax({url:"post.aspx",type:"post",datatype:"html",data:"id=10",success:function(msg){if(msg=="true"){alert("保存成功!");}else{alert("保存失败!");}}});

这就是用post方法将id传到post.aspx去了。post.aspx处理完以后,直接返回一个字符串,如果成功则返回"true",如果失败随便返回什么,或者错误信息。
lovexilove 2009-12-29
  • 打赏
  • 举报
回复
request.form["...."] 来取传过来的值
wujinjian2008n 2009-12-29
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 happyboyxq 的回复:]
上面已经都说完了,不过对于后面Default的html页,最好把代码都删掉,不然你读出来的数据可能不是你想直接返回的数据。
[/Quote]

加一句 Response.End() 就好了
wujinjian2008n 2009-12-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zmcici 的回复:]
都说了是post方法,还在ULR上传值,那样不就是GET方法了,我会做。本页面是Default1.aspx
Default2.aspx是处理Ajax的页面。数据通过post方式提交到Default2.aspx,在Default2.aspx做提交到数据库的操作。在返回处理结果到Default1.aspx,实现无刷新操作
[/Quote]

你从哪看到是从url上传了?
happyboyxq 2009-12-29
  • 打赏
  • 举报
回复
上面已经都说完了,不过对于后面Default的html页,最好把代码都删掉,不然你读出来的数据可能不是你想直接返回的数据。
骑猪看海 2009-12-29
  • 打赏
  • 举报
回复
都说了是post方法,还在ULR上传值,那样不就是GET方法了,我会做。本页面是Default1.aspx
Default2.aspx是处理Ajax的页面。数据通过post方式提交到Default2.aspx,在Default2.aspx做提交到数据库的操作。在返回处理结果到Default1.aspx,实现无刷新操作
wujinjian2008n 2009-12-29
  • 打赏
  • 举报
回复
建两个页面,一个是 Default.aspx 一个是 AjaxData.aspx
wujinjian2008n 2009-12-29
  • 打赏
  • 举报
回复
特意帮你敲了一个:
Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="AjaxData" %>

<!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">
<title>无标题页</title>
<script type="text/javascript">

var xmlHttp;

function createXMLHttpRequest()
{
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}

function postTJ(str)
{
createXMLHttpRequest();

xmlHttp.open("post","AjaxData.aspx",true);
xmlHttp.onreadystatechange=stateChange;

xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");

xmlHttp.send("str="+str+"&date="+new Date().getTime());
}

function stateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
alert(xmlHttp.responseText);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="postTJ('wujj')" value="Post">
</div>
</form>
</body>
</html>



AjaxData.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.Form["str"]+"--好好学习");

Response.End();
}
}

mhl0410 2009-12-29
  • 打赏
  • 举报
回复
我这里有一个jqery的无刷新跳转页面的例子,不过没有将值传掉另外的一个页面。是无刷新的显示评论页面
lemong 2009-12-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 silentwins 的回复:]
post到Default2.aspx的url就是Default2.aspx?userName=yourusername&psw=yourpassword

在后台的Page_Load用Request.Form["userName"]获取参数数值
[/Quote]

这样就可以了,都在PageLoad里取
phoeni_xin 2009-12-29
  • 打赏
  • 举报
回复
mark
silentwins 2009-12-29
  • 打赏
  • 举报
回复
post到Default2.aspx的url就是Default2.aspx?userName=yourusername&psw=yourpassword

在后台的Page_Load用Request.Form["userName"]获取参数数值
阿非 2009-12-29
  • 打赏
  • 举报
回复
利用XMLHttpRequest对象异步发送XML文档

http://blog.csdn.net/Sandy945/archive/2009/05/12/4170512.aspx
l171147904 2009-12-29
  • 打赏
  • 举报
回复
..........
l171147904 2009-12-29
  • 打赏
  • 举报
回复
后台也就是Default2.aspx页面???
这文件是 后台吗?

不用自带的,那你用 AJAX2.0 ,去下一个DLL 引用就行

62,046

社区成员

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

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

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

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