用juery的ajax方法调用aspx.cs页面中的webmethod方法示例
如:
[WebMethod]
public static string GetUserName()
{
//......
}
如果要在这个方法里操作session,那还得将WebMethod的EnableSession 属性设为true 。即:
[WebMethod(EnableSession = true)]//或[WebMethod(true)]
public static
string GetUserName()
{
//......
}
然后我们就写ajax程序来访问这个程序,我们就用jQuery吧。
代码如下:
type: "POST",
contentType:
"application/json",
url: "WebForm2.aspx/GetUserName",
data: "{}",
dataType: "json",
success: function(){.......}
});
type:请求的类型,这里必须用post 。WebMethod方法只接受post类型的请求。
contentType:发送信息至服务器时内容编码类型。我们这里一定要用 application/json 。
url:请求的服务器端处理程序的路径,格式为"文件名(含后缀)/方法名"
data:参
数列表。注意,这里的参数一定要是json格式的字符串,记住是字符串格式,如:"{aa:11,bb:22,cc:33 ,
...}"。如果你写的不是字符串,那jquery会把它实序列化成字符串,那么在服务器端接受到的就不是json格式了,且不能为空,即使没有参数也要
写成"{}",如上例。
很多人不成功,原因就在这里。
dataType:服务器返回的数据类型。必须是json,其他的都无效。因为
webservice 是一json格式返回数据的,其形式为:{"d":"......."}。
success:请求成功后的回调函数。你
可以在这里对返回的数据做任意处理。
下面给个ajax请求自身页面的例子给你测试。。。
test.aspx
XML/HTML code
代码如下:
<script runat="server">
protected void Page_Load(object
sender,EventArgs e){
Response.Charset="gb2312";
if(Request.Form["method"]=="Test")Test();
else
if(Request.Form["method"]=="Test1")Test1();
else
if(Request.Form["method"]=="Test2")Test2();
Response.Write("一般请求<br/>");
}
public void Test()
{
Response.Write("执行Test方法"+DateTime.Now);
Response.End();//停止其他输出
}
public void Test1()
{
Response.Write("执行Test1方法"+DateTime.Now);
Response.End();//停止其他输出
}
public void Test2()
{
Response.Write("执行Test2方法"+DateTime.Now);
Response.End();//停止其他输出
}
</script>
<!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">
<meta http-equiv="content-type"
content="text/html;charset=gb2312" />
<script type="text/javascript"
src="jquery.js"></script>
</head>
<body>
<input type="button" value="调用Test"
onclick="CallMethod('Test')"/><input type="button" value="调用Test1"
onclick="CallMethod('Test1')"/><input type="button" value="调用Test2"
onclick="CallMethod('Test2')"/>
<script type="text/javascript">
function CallMethod(method){
$.ajax(
{
type: "POST",
url:
"test.aspx",
data:{method:method},
success:function(msg){alert(msg);},
error: function(){alert('出错了');}
}
)
}
$(document).ready(function(){
$.ajax(
{
type: "POST",
url:
"test.aspx",
data:{method:"Test"},
success:function(msg){alert("$(document).ready执行方法Test返回结果\n\n\n"+msg);},
error: function(){alert('出错了');}
}
);
})
</script>
</body>
</html>
用juery的ajax方法调用aspx.cs页面中的webmethod方法示例的更多相关文章
- 用juery的ajax方法调用aspx.cs页面中的webmethod方法
首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性. 如: [WebMethod] public static string GetUserName() { //.... ...
- WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据
WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据 WebForm1.aspx 页面 (原生AJAX请求,写法一) <%@ Page Langu ...
- WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据(转)
WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据 WebForm1.aspx 页面 (原生AJAX请求,写法一) <%@ Page Langu ...
- Javscript调用iframe框架页面中函数的方法
Javscript调用iframe框架页面中函数的方法,可以实现iframe之间传值或修改值了, 访问iframe里面的函数: window.frames['CallCenter_iframe'].h ...
- jQuery Ajax 方法调用 Asp.Net WebService 以及调用aspx.cs中方法的详细例子
一.jQuery Ajax 方法调用 Asp.Net WebService (引自Terry Feng) Html文件 <!DOCTYPE html PUBLIC "-//W3C//D ...
- jQuery 互相调用iframe页面中js的方法
1,子iframe内调用父类函数方法: window.parent.func(); 2,子Iframe中获取父界面的元素: $("#xx", window.parent.docum ...
- JQuery调用iframe子页面函数/对象的方法例子
父页面有个ID为mainfrm.name为Iframe1的iframe,iframe连接b.html,该页面有个函数test 在父页面调用b.html的test方法为: $("#mainfr ...
- 被synchronized修饰的方法调用了没有被synchronized修饰的方法,是否是线程安全
1 被synchronized修饰的方法调用了没有被synchronized修饰的方法,是否线程安全? /** * (1)被synchronized修饰的方法调用了没有被synchronized修饰的 ...
- javascript 获取父页面中元素对象方法
父页面中: <input type="hidden" id="areaID" value="test1"> <iframe ...
随机推荐
- Android开发之Ubuntu上Eclipse不显示手机设备
一.准备工作 A.开启Android设备,用USB数据线连接到Ubuntu系统. B.启用设备的USB调试模试 C.启动Eclipse,在Devices栏会现一个有很多???号的不明设备 ...
- Dede推荐文章与热点文章不显示?
)先看看,id 文档ID,content 标签最终内容(修改此变量后必须更新系统缓存)是不是填写的id: 进入后台->系统->基本参数设置->性能选项->id 文档ID,con ...
- log4net 开箱即用
废话少说,先上代码 log4net Demo 好的系统都有日志,log4net 是我在.net平台下用过最爽的日志库,简单易用.功能强大. 基于配置(配置很简单,一看就明,通用,拷去即用): 可同时保 ...
- 通过python为zabbix发送告警邮件
最近部署ZABBIX的邮件告警时,用刚学的python来写告警邮件脚本. 由于时间有限,我只对关键步骤做截图,对zabbix的基本配置略过. python代码如下 1 #!/usr/bin/pytho ...
- js判断移动端与pc端
这里介绍下使用device.js插件来判断移动端设备 地址:https://github.com/matthewhudson/device.js 示例: if(device.mobile()){ wi ...
- Linux系统下给非root用户添加sudo权限
Linux系统下给非root用户添加sudo权限 有时,在linux系统中非root用户运行sudo命令,会提示类似信息: xxx is not in the sudoers file. This ...
- Jquery下控制backgroundPosition位置的问题/jquery查找data id相等的元素
1:想通过控制backgroundPosition 来实现动态效果: $(this).animate({"background-positionY":-13},1); //但是火狐 ...
- IPython与notebook 安装
基于 python2.7.13 32-bit版本安装 1.安装pyreadline https://pypi.python.org/pypi/pyreadline 下载对应的32位版本 2.用pip安 ...
- Reprint: Serialization
Having just recently ran into some major serialization issues I’m going to list some of the errors a ...
- 【Time系列四】查询各月份的日历
荒废了两个星期没学java了,今天一心想突破"日历查询"这个小程序.先用比较简单的python实现下. 无奈天资愚钝,想了一个上午.最后卡在了日期排列的问题上,只好去参考下别人的代 ...