首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性。 
如: 
[WebMethod] 
public static string GetUserName() 

//...... 

  如果要在这个方法里操作session,那还得将WebMethod的EnableSession 属性设为true 。即: 
[WebMethod(EnableSession = true)]//或[WebMethod(true)] 
public static string GetUserName() 

//...... 

 然后我们就写ajax程序来访问这个程序,我们就用jQuery吧。 
代码如下:

$.ajax({ 
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 
代码如下:

%@ Page language="C#"% 
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返回结果nnn"+msg);}, 
error: function(){alert('出错了');} 

); 
}) 
/script 
/body 
/html

用juery的ajax方法调用aspx.cs页面中的webmethod方法的更多相关文章

  1. 用juery的ajax方法调用aspx.cs页面中的webmethod方法示例

    juery的ajax调用aspx.cs页面中的webmethod方法:首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性,具体实现如下,感兴趣的朋友可以参考下哈,希望对大 ...

  2. WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据

    WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据 WebForm1.aspx 页面 (原生AJAX请求,写法一) <%@ Page Langu ...

  3. WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据(转)

    WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据 WebForm1.aspx 页面 (原生AJAX请求,写法一) <%@ Page Langu ...

  4. Javscript调用iframe框架页面中函数的方法

    Javscript调用iframe框架页面中函数的方法,可以实现iframe之间传值或修改值了, 访问iframe里面的函数: window.frames['CallCenter_iframe'].h ...

  5. jQuery Ajax 方法调用 Asp.Net WebService 以及调用aspx.cs中方法的详细例子

    一.jQuery Ajax 方法调用 Asp.Net WebService (引自Terry Feng) Html文件 <!DOCTYPE html PUBLIC "-//W3C//D ...

  6. jQuery 互相调用iframe页面中js的方法

    1,子iframe内调用父类函数方法: window.parent.func(); 2,子Iframe中获取父界面的元素: $("#xx", window.parent.docum ...

  7. JQuery调用iframe子页面函数/对象的方法例子

    父页面有个ID为mainfrm.name为Iframe1的iframe,iframe连接b.html,该页面有个函数test 在父页面调用b.html的test方法为: $("#mainfr ...

  8. 被synchronized修饰的方法调用了没有被synchronized修饰的方法,是否是线程安全

    1 被synchronized修饰的方法调用了没有被synchronized修饰的方法,是否线程安全? /** * (1)被synchronized修饰的方法调用了没有被synchronized修饰的 ...

  9. javascript 获取父页面中元素对象方法

    父页面中: <input type="hidden" id="areaID" value="test1"> <iframe ...

随机推荐

  1. c# 小数四舍五入,向上取整,向下取整,见角进元保留多个小数位数

    /// <summary> /// 实现数据的四舍五入法 /// </summary> /// <param name="v">要进行处理的数据 ...

  2. Spring Boot:The field file exceeds its maximum permitted size of 1048576 bytes

    错误信息:The field file exceeds its maximum permitted size of 1048576 bytes原因是因为SpringBoot内嵌tomcat默认所能上传 ...

  3. redis 初步认识三(设置登录密码)

    1.cmd 2.cd C:\Program Files\Redis 3.redis-cli.exe -h 127.0.0.1 -a 123456

  4. Linux内存管理 (23)一个内存Oops解析

    专题:Linux内存管理专题 关键词:DataAbort.fsr.pte.backtrace.stack.   在内存相关实际应用中,内存异常访问是一种常见的问题. 本文结合异常T32栈回溯.Oops ...

  5. TensorRT&Sample&Python[end_to_end_tensorflow_mnist]

    本文是基于TensorRT 5.0.2基础上,关于其内部的end_to_end_tensorflow_mnist例子的分析和介绍. 1 引言 假设当前路径为: TensorRT-5.0.2.6/sam ...

  6. 删除a表中和b表相同的数据

    删除a表中和b表相同的数据 - 冯索的专栏 - CSDN博客https://blog.csdn.net/wugouzi/article/details/9374329 oracle 查找A表存在B表不 ...

  7. 【apache】No input file specified

    默认的 RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]规则在apache fastcgi模式下会导致No input file specified. 修改成 Re ...

  8. CKEditor 4.5 filetools, XHR.withCredentials = true,

    var editor = CKEDITOR.replace( 'editor1', { extraPlugins: 'uploadimage,filetools', imageUploadUrl: ' ...

  9. springboot 集成swagger

    了解到swagger 就记录下用法 pom.xml <dependency> <groupId>io.springfox</groupId> <artifac ...

  10. Python——Django-form表单提交

    一.提交的注意事项 1. form不是from,所有获取用户输入的标签都应该放在form里面, input并且必须要有name属性 2. action属性控制往哪儿提交,method一般都设置成pos ...