Asp.net可以在aspx页面中添加ScriptManager,这样就允许将WebMethod添加在aspx页面中,而以往通常都是新建asmx文件,利用webservice提供webmethod供请求。这里列出利用ajax请求aspx页面中的webmethod的方法,当然也可以利用ScriptManager创建的PageMethods的js对象直接访问后台代码中的webmethod。
linktest.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="linktest.aspx.cs" Inherits="linktest" %> <!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" src="MyJs/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="MyJs/jquery.json-2.3.min.js"></script> <script type="text/javascript"> function g() { var flag = false; $.ajax({ type: "POST", async: false, url: "linktest.aspx/InsertComment", //data: $.toJSON({ "thecontent1": 'hello' }), data: window.JSON.stringify({ "thecontent1": 'hello' }), contentType: "application/json; charset=utf-8", dataType: "json", error: function() { alert('出错了'); }, success: function(json) { if (json.d) { alert("successs"); flag = true; } else { alert("kao"); } } }); return flag; } function f() { $.ajax({ type: "POST", async: true, url: "linktest.aspx/GetJsonObject", data: $.toJSON({ "p": 'hello' }), //data: window.JSON.stringify({ "thecontent1": 'hello' }), contentType: "application/json; charset=utf-8", dataType: "json", error: function() { alert('出错了'); }, success: function(json) { alert("" + json.d.key1 + json.d.keyofarray[2] + json.d.keyofobject.subkey); } }); } function h() { //不通过ajax,直接通过scriptmanager访问后台的pagemethod PageMethods.TestMethod("dlut", OnSucceeded1, OnFailed); //后两个参数为请求成功和失败的回调函数 } function OnSucceeded1(result, userContext, methodName) { if (methodName == "TestMethod") { alert(result); } } function OnFailed(error, userContext, methodName) { if (error !== null) { alert("error.."); } } </script> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="sm1" EnablePageMethods="true" runat="server" /> <div> <a href="http://www.baidu.com" target="_blank" onclick="return g();">Go to baidu..</a> <br /> <a href="http://www.sina.com.cn" target="_blank" onclick="f();">Go to sina..</a> <br /> <input type="button" onclick="h();" value="Request PageMethod" /> </div> </form> </body> </html>
这里写了3个测试,前两个是通过Jquery的ajax,最后一个直接通过PageMethods请求pagemethod。要知道,aspx后台要想在新标签页中打开一个链接可有点不容易,这里利用A标签onclick里Ajax请求,如果onclick返回false,则不会跳转。
还要注意的是$.ajax的data参数,因为设置成了json格式,所以利用$.toJSON将js对象转换成json对象,其实用window.JSON.stringify也行,但是这个不是所有浏览器都奏效。
前台往后台传参数这样将js对象转换成json就行,只要里面的属性和后台的webmethod的参数对应就行,如果后台的webmethod的参数是一个C#对象,同样的,只要js对象的属性和C#对象的属性对应就可以了。
还要注意的就是ajax的async标志,这个很好理解。前台得到返回结果以后取得返回值记住是在json.d下。
后台代码linktest.aspx.cs代码如下:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Collections.Generic; public partial class linktest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Session["test"] = "hey"; } } [System.Web.Services.WebMethod] public static bool InsertComment(string thecontent1) { return thecontent1.Equals("hello"); } [System.Web.Services.WebMethod] public static string TestMethod(string p) { return p + " data from back.."; } [System.Web.Services.WebMethod] public static SortedDictionary<string, object> GetJsonObject(string p) { SortedDictionary<string, object> results = new SortedDictionary<string, object>(); results.Add("key1","value1"); results.Add("key2", p); results.Add("keyofarray",new int[]{1, 2, 3}); SortedDictionary<string, object> subitem = new SortedDictionary<string, object>(); subitem.Add("subkey","hah"); results.Add("keyofobject", subitem); return results; } [System.Web.Services.WebMethod(EnableSession=true)] public static SortedDictionary<string, object> ObjectParameterTest(Books p) { SortedDictionary<string, object> results = new SortedDictionary<string, object>(); results.Add("key1", p.bookTitle); results.Add("key2", HttpContext.Current.Session["test"].ToString() ); return results; } }
最后一个ObjectParameterTest方法的参数是一个C#对象,前台传递的时候只要json的属性跟它对应就可以了。这里还展示了怎样取到session。
相关推荐
juery的ajax调用aspx.cs页面中的webmethod方法:首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性,具体实现如下,感兴趣的朋友可以参考下哈,希望对大家有所帮助
dotNet下WebMethod属性[文].pdf
webmethod draft introduce for webmethod
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下。 (1)通过aspx.cs的静态方法+WebMethod进行处理 简单的介绍下WebMethod方法的用法 1.修饰符主要用public static修饰...
用户注册实例,轻松学会webmethod
webmethod demo, BPMS it's a draft version
主要介绍了甩掉ashx和asmx使用jQuery.ajaxWebService请求WebMethod简练处理Ajax的相关资料,需要的朋友可以参考下
ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例 .
主要介绍了jQuery调用WebMethod(PageMethod) NET2.0的方法,结合实例形式分析了jQuery基于ajax调用.NET方法的相关技巧,需要的朋友可以参考下
import javax.jws.WebMethod; import javax.xml.ws.Endpoint; @WebService public class Hello { @WebMethod public String hello(String name) { return "Hello, " + name + "\n"; } public static ...
1.新建demo.aspx页面。 2.首先在该页面的后台文件demos.aspx.cs中添加引用。 using System.Web.Services; 1).无参数的方法调用. 大家注意了,这个版本不能低于.net framework 2.0。2.0已下不支持的。 后台代码: ...
再aspx后台的页面编写一个简单的方法,代码如下: 代码如下: [WebMethod] public static string ABC(string ABC) { return ABC; } 必须声明为静态方法,并且它们必须使用 [WebMethod] 特性标注。但是在webservice里面...
在这里,为方便开发,我新建了一个页面专门用于写WebMethod方法.那样会比较方便,也比较好管理. 如果ajax请求比较多,可以多建几个页面.根据页面的名称来作下请求的分类 例,下面贴出后台代码: /// <summary
首先想到的是在每一个带有WebMethod特性的方法里调用记录请求信息的方法,这样可以记录信息,但是太多带WebMethod特性的方法了,于是想在全局中拦截并捕获,于是想到了Global.asax public class Global : System....
基于Jquery与WebMethod投票功能实现代码,需要的朋友可以参考下。
摘要WebMethod通过封装大量的功能使得XMLWeb服务的开发更加容易,但是仍然有许多基础的XML处理需要由用户来负责。例如,WebMethod不会根据隐含架构对消息进行验证。因为没有对这些消息进行验证,所以返回的响应可能...
1.新建demo.aspx页面。 2.首先在该页面的后台文件demos.aspx.cs中添加引用。 using System.Web.Services; 3.无参数的方法调用.大家注意了,这个版本不能低于.net framework 2.0。2.0已下不支持的。 后台代码: 代码...
webmethods 6.5 安装手册英文版