新瓶旧酒ASP.NET AJAX(9) - 客户端脚本编程(Sys.Net命名空间下的WebServiceProxy)-阿里云开发者社区

开发者社区> 技术小胖子> 正文

新瓶旧酒ASP.NET AJAX(9) - 客户端脚本编程(Sys.Net命名空间下的WebServiceProxy)

简介:
+关注继续查看




[索引页]

[源码下载]


新瓶旧酒ASP.NET AJAX(9) - 客户端脚本编程(Sys.Net命名空间下的WebServiceProxy、WebServiceError、Generated Proxy Classes以及调用WebService、PageMethod)


作者:webabcd


介绍
使用ASP.NET AJAX调用WebService(简单类型和复杂对象、JSON和XML)和PageMethod。Sys.Net命名空间下的WebServiceProxy Class、WebServiceError Class、Generated Proxy Classes


关键
1、调用WebService
    ·为WebService类或需要暴露给客户端的WebService方法添加[System.Web.Script.Services.ScriptService]属性
    ·为WebService类中需要暴露给客户端使用的方法添加[System.Web.Services.WebMethod]属性
    ·在ScriptManager控件的Services集合中添加对该WebService的引用
    ·客户端调用方法:[NameSpace].[ClassName].[MethodName](param1, param2, ..., succeededCallback, failedCallback, userContext)
    ·如果用GET方式的话,则在WebService方法添加[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]属性
    ·用到了复杂类型的话,则在WebService类上添加[System.Web.Script.Services.GenerateScriptType(typeof(TypeName))]属性。另外,该复杂类型必须要有一个无参数的构造函数
    ·禁止将某属性输出到客户端,则在该属性上添加[System.Web.Script.Serialization.ScriptIgnore]属性
    ·以XML方式输出到客户端(一般是JSON),则在WebService方法添加[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]属性。如果要避免ASP.NET AJAX为我们做自动转换,则应该添加[ScriptMethod(ResponseFormat = ResponseFormat.Xml, XmlSerializeString = true)]属性
    ·以XML方式输出到客户端的复杂对象的属性,在属性上添加[System.Xml.Serialization.XmlAttribute]属性(可不写)。若禁止将其输出到客户端,则在属性上添加[System.Xml.Serialization.XmlIgnore]属性。
    ·WebService判断用户是否通过验证 - HttpContext.Current.User.Identity.IsAuthenticated
    ·WebService使用Session - HttpSessionState session = HttpContext.Current.Session;

2、调用PageMethod
    ·ASP.NET页面中的方法需要时Public和Static的
    ·为该方法添加[System.Web.Services.WebMethod]属性
    ·设置ScriptManager控件的EnablePageMethods="true"
    ·客户端调用方法:PageMethods.[MethodName](param1, param2, ..., callbackFunction);

3、WebServiceProxy Class
    var webRequest = Sys.Net.WebServiceProxy.invoke(path, methodName, useHttpGet, parameters, succeededCallback, failedCallback, userContext, timeout);
    ·path - WebService的url
    ·methodName - 调用的方法名
    ·useHttpGet - 是否使用HTTP GET方式
    ·parameters - 参数(示例:{"param1":196610, "param2":"Hello"})
    ·succeededCallback - 调用成功的回调函数
    ·failedCallback - 调用失败的回调函数
    ·userContext - 用户上下文
    ·timeout - 超时时间
    ·返回值 - 相关的WebRequest对象

4、WebServiceError Class
    ·exceptionType - 服务器端异常的具体类型
    ·message - 详细的异常描述信息
    ·stackTrace - 服务器端异常的堆栈跟踪信息
    ·statusCode - 造成异常的HTTP响应的状态码
    ·timedOut - 异常是否是由于网络连接超时造成的

5、Generated Proxy Classes属性
    ·defaultSucceededCallback - 调用成功的回调函数
    ·defaultFailedCallback - 调用失败的回调函数
    ·defaultUserContext - 用户上下文
    ·path - WebService的路径
    ·timeout - 超时时间

6、Generated Proxy Classes(调用Web Service)
    var myServiceProxy = MyNameSpace.MyService();
    myServiceProxy.MyServiceMethod(param1, param2, ..., succeededCallback, failedCallback, userContext);
    ·param - 在前面顺序地写参数
    ·succeededCallback - 调用成功的回调函数
    ·failedCallback - 调用失败的回调函数
    ·userContext - 调用成功的回调函数

7、Generated Proxy Classes(调用成功的回调函数)
    function succeededCallback(result, userContext, methodName) { }
    ·result - 调用WebService的方法后返回的数据
    ·userContext - 用户上下文
    ·methodName - 调用的方法名

8、Generated Proxy Classes(调用失败的回调函数)
    function failedCallback(error, userContext, methodName) { }
    ·error - 调用WebService的方法后返回的数据(Sys.Net.WebServiceError对象)
    ·userContext - 用户上下文
    ·methodName - 调用的方法名


示例
Hello.asmx
InBlock.gif<%@ WebService Language="C#" Class="Hello" %> 
InBlock.gif 
InBlock.gifusing System; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Services; 
InBlock.gifusing System.Web.Services.Protocols; 
InBlock.gifusing System.Web.Script.Services; 
InBlock.gif 
InBlock.gif[WebService(Namespace = "http://tempuri.org/")] 
InBlock.gif[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
InBlock.gif[ScriptService] 
InBlock.gifpublic class Hello : System.Web.Services.WebService 
InBlock.gif
InBlock.gif        [WebMethod] 
InBlock.gif        // 如果用GET方式的话,[ScriptMethod(UseHttpGet = true)] 
InBlock.gif        public string Say(string name) 
InBlock.gif        { 
InBlock.gif                return String.Format("WebService Hello {0}", name); 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        [WebMethod] 
InBlock.gif        // 如果用GET方式的话,[ScriptMethod(UseHttpGet = true)] 
InBlock.gif        public int SayError(int a, int b) 
InBlock.gif        { 
InBlock.gif                return a / b; 
InBlock.gif        } 
InBlock.gif}
 
Hello.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" Title="WebService、PageMethod、WebServiceError Class" %> 

<%@ Import Namespace="System.Web.Services" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 

        <script runat="Server"> 
                 
                [WebMethod] 
                public static string Say(string name) 
                { 
                        return String.Format("PageMethod Hello {0}", name); 
                } 

        </script> 

        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"> 
                <services> 
                        <asp:ServiceReference Path="Hello.asmx" /> 
                </services> 
        </asp:ScriptManagerProxy> 
        <input type="text" id="txtName" value="webabcd" /> 
        <input type="button" id="btnHelloWebService" value="HelloWebService" /> 
        <input type="button" id="btnHelloPageMethod" value="HelloPageMethod" /> 
        <input type="button" id="btnContext" value="HelloContext" /> 
        <input type="button" id="btnHelloError" value="HelloError" /> 
        <br /><br /> 
        <div id="result" /> 

        <script type="text/javascript"> 
                function btnHelloWebService_click()    
                { 
                        var name = $get("txtName").value; 
                        Hello.Say(name, onSucceeded); 
                } 

                function btnHelloPageMethod_click()    
                { 
                        var name = $get("txtName").value; 
                        PageMethods.Say(name, onSucceeded); 
                } 
                 
                function btnHelloContext_click()    
                { 
                        var name = $get("txtName").value; 
                        Hello.Say(name, onHelloContextSucceeded, null, jsonContext); 
                } 

                function btnHelloError_click()    
                { 
                        Hello.SayError(100, 0, onSucceeded, onFailed); 
                } 

                function onSucceeded(result)    
                { 
                        $get("result").innerHTML = result + "<br />"; 
                } 
                 
                function onHelloContextSucceeded(result, context)    
                { 
                        $get("result").innerHTML = result + "<br />"; 
                         
                        $get("result").innerHTML += context.Name + "<br />"; 
                        $get("result").innerHTML += context.Age + "<br />"; 
                } 
                 
                function onFailed(error)    
                { 
                        // error - Sys.Net.WebServiceError对象 
                 
                        // exceptionType - 服务器端异常的具体类型 
                        $get("result").innerHTML = "Exception Type:" + error.get_exceptionType() + "<br />"; 
                        // message - 详细的异常描述信息 
                        $get("result").innerHTML += "Message:" + error.get_message() + "<br />"; 
                        // stackTrace - 服务器端异常的堆栈跟踪信息 
                        $get("result").innerHTML += "Stack Trace:" + error.get_stackTrace() + "<br />"; 
                        // statusCode - 造成异常的HTTP响应的状态码 
                        $get("result").innerHTML += "Status Code:" + error.get_statusCode() + "<br />"; 
                        // timedOut - 异常是否是由于网络连接超时造成的 
                        $get("result").innerHTML += "Timed Out:" + error.get_timedOut() + "<br />"; 
                } 
                 
                var jsonContext =    
                { 
                        Name: "webabcd", 
                        Age: 27 
                }; 

        </script> 

</asp:Content>
 
 
运行结果
1、单击“HelloWebService”按钮
WebService Hello webabcd

2、单击“HelloPageMethod”按钮
PageMethod Hello webabcd

3、单击“HelloContext”按钮
WebService Hello webabcd
webabcd
27

4、单击“HelloError”按钮
Exception Type:System.DivideByZeroException
Message:试图除以零。
Stack Trace: 在 Hello.SayError(Int32 a, Int32 b) 位置 c:\Documents and Settings\wanglei\桌面\AJAX\Web\WebService\Hello.asmx:行号 25
Status Code:500
Timed Out:false


Sample.asmx
InBlock.gif<%@ WebService Language="C#" Class="Sample" %> 
InBlock.gif 
InBlock.gifusing System; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Services; 
InBlock.gifusing System.Web.Services.Protocols; 
InBlock.gifusing System.Web.Script.Services; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gifusing System.Data; 
InBlock.gif 
InBlock.gif[WebService(Namespace = "http://tempuri.org/")] 
InBlock.gif[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
InBlock.gif[GenerateScriptType(typeof(Person))] 
InBlock.gif[ScriptService] 
InBlock.gifpublic class Sample : System.Web.Services.WebService 
InBlock.gif
InBlock.gif        [WebMethod] 
InBlock.gif        public string SimpleTypeTest( 
InBlock.gif                string stringTest, bool boolTest, int intTest, float floatTest, 
InBlock.gif                DateTime datetimeTest, DayOfWeek enumTest) 
InBlock.gif        { 
InBlock.gif                return String.Format("stringTest:{0};boolTest:{1};intTest:{2};floatTest:{3};datetimeTest:{4};enumTest:{5}"
InBlock.gif                        stringTest, boolTest, intTest, floatTest, datetimeTest, enumTest); 
InBlock.gif        } 
InBlock.gif 
InBlock.gif 
InBlock.gif        [WebMethod] 
InBlock.gif        public Person GetPerson() 
InBlock.gif        { 
InBlock.gif                return new Person("webabcd", 27); 
InBlock.gif        } 
InBlock.gif        [WebMethod] 
InBlock.gif        public string SetPerson(Person p) 
InBlock.gif        { 
InBlock.gif                return String.Format("Name:{0};Age:{1}", p.Name, p.Age); 
InBlock.gif        } 
InBlock.gif 
InBlock.gif 
InBlock.gif        [WebMethod] 
InBlock.gif        public List<Person> GetList() 
InBlock.gif        { 
InBlock.gif                List<Person> list = new List<Person>(); 
InBlock.gif 
InBlock.gif                for (int i = 0; i < 10; i++) 
InBlock.gif                { 
InBlock.gif                        Person p = new Person(i.ToString().PadLeft(4, '0'), i + 20); 
InBlock.gif                        list.Add(p); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                return list; 
InBlock.gif        } 
InBlock.gif        [WebMethod] 
InBlock.gif        public string SetList(List<Person> list) 
InBlock.gif        { 
InBlock.gif                StringBuilder sb = new StringBuilder(); 
InBlock.gif 
InBlock.gif                sb.Append("<table border='1'>"); 
InBlock.gif                foreach (Person p in list) 
InBlock.gif                { 
InBlock.gif                        sb.Append("<tr>"); 
InBlock.gif                        sb.Append("<td>Name:" + p.Name + "</td>"); 
InBlock.gif                        sb.Append("<td>Age:" + p.Age + "</td>"); 
InBlock.gif                        sb.Append("</tr>"); 
InBlock.gif                } 
InBlock.gif                sb.Append("</table>"); 
InBlock.gif 
InBlock.gif                return sb.ToString(); 
InBlock.gif        } 
InBlock.gif 
InBlock.gif 
InBlock.gif        [WebMethod] 
InBlock.gif        public Dictionary<string, Person> GetDictionary() 
InBlock.gif        { 
InBlock.gif                Dictionary<string, Person> dictionary = new Dictionary<string, Person>(); 
InBlock.gif 
InBlock.gif                for (int i = 0; i < 10; i++) 
InBlock.gif                { 
InBlock.gif                        Person p = new Person(i.ToString().PadLeft(4, '0'), i + 20); 
InBlock.gif 
InBlock.gif                        dictionary.Add(i.ToString(), p); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                return dictionary; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif 
InBlock.gif        [WebMethod] 
InBlock.gif        public DataTable GetDataTable() 
InBlock.gif        { 
InBlock.gif                DataTable dt = new DataTable(); 
InBlock.gif 
InBlock.gif                dt.Columns.Add(new DataColumn("Name"typeof(string))); 
InBlock.gif                dt.Columns.Add(new DataColumn("Age"typeof(int))); 
InBlock.gif 
InBlock.gif                for (int i = 0; i < 10; i++) 
InBlock.gif                { 
InBlock.gif                        DataRow dr = dt.NewRow(); 
InBlock.gif                        dr["Name"] = i.ToString().PadLeft(4, '0'); 
InBlock.gif                        dr["Age"] = i + 20; 
InBlock.gif 
InBlock.gif                        dt.Rows.Add(dr); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                return dt; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif 
InBlock.gif        [WebMethod] 
InBlock.gif        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)] 
InBlock.gif        // 避免ASP.NET AJAX为我们做自动转换,[ScriptMethod(ResponseFormat = ResponseFormat.Xml, XmlSerializeString = true)] 
InBlock.gif        public List<Person> GetXml() 
InBlock.gif        { 
InBlock.gif                return GetList(); 
InBlock.gif        } 
InBlock.gif}
 
Sample.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" Title="WebService(简单类型和复杂对象、JSON和XML)" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"> 
                <services> 
                        <asp:ServiceReference Path="Sample.asmx" /> 
                </services> 
        </asp:ScriptManagerProxy> 
        <input type="button" id="btnSimpleTypeTest" value="SimpleTypeTest" /> 
        <input type="button" id="btnGetPerson" value="GetPerson" /> 
        <input type="button" id="btnSetPerson" value="SetPerson" /> 
        <input type="button" id="btnGetList" value="GetList" /> 
        <input type="button" id="btnSetList" value="SetList" /> 
        <input type="button" id="btnGetDictionary" value="GetDictionary" /> 
        <input type="button" id="btnGetDataTable" value="GetDataTable" /> 
        <input type="button" id="btnGetXml" value="GetXml" /> 
        <br /><br /> 
        <div id="result" /> 

        <script type="Text/javascript"> 
                function btnSimpleTypeTest_click()    
                { 
                        Sample.SimpleTypeTest("abc", false, 1, 1.1, new Date(), System.DayOfWeek.Sunday, onSimpleTypeTestSucceeded); 
                } 
                function onSimpleTypeTestSucceeded(result)    
                { 
                        $get("result").innerHTML = result + "<br />"; 
                } 
                 
                 
                function btnGetPerson_click()    
                { 
                        Sample.GetPerson(onGetPersonSucceeded); 
                } 
                function onGetPersonSucceeded(result)    
                { 
                        $get("result").innerHTML = String.format("Name:{0};Age:{1}<br />", result.Name, result.Age); 
                } 
                 
                 
                function btnSetPerson_click()    
                { 
                        // var p = new Object(); 

                        var p = new Person(); 
                        p.Name = "webabcd"; 
                        p.Age = 27; 
                        Sample.SetPerson(p, onSetPersonSucceeded); 
                } 
                function onSetPersonSucceeded(result) 
                { 
                        $get("result").innerHTML = result + "<br />"; 
                } 
                 
                 
                function btnGetList_click()    
                { 
                        Sample.GetList(onGetListSucceeded); 
                } 
                function onGetListSucceeded(result)    
                { 
                        var sb = new Sys.StringBuilder(); 
                         
                        sb.append("<table border='1'>"); 
                        for (var i=0; i<result.length; i++) 
                        { 
                                sb.append("<tr>"); 
                                sb.append("<td>Name:" + result[i].Name + "</td>"); 
                                sb.append("<td>Age:" + result[i].Age + "</td>"); 
                                sb.append("</tr>"); 
                        } 
                        sb.append("</table>"); 

                        $get("result").innerHTML = sb.toString(); 
                } 
                 
                 
                function btnSetList_click()    
                { 
                        var ary = new Array(); 
                        for (var i = 0 ; i < 10; i++)    
                        { 
                                var p = new Person(); 
                                p.Name = i + 1000; 
                                p.Age = i + 20; 
                                 
                                Array.add(ary, p); 
                        } 

                        Sample.SetList(ary, onSetListSucceeded); 
                } 
                function onSetListSucceeded(result)    
                { 
                        $get("result").innerHTML = result; 
                } 
                 
                 
                function btnGetDictionary_click()    
                { 
                        Sample.GetDictionary(onGetDictionarySucceeded); 
                } 
                function onGetDictionarySucceeded(result)    
                {                 
                        var sb = new Sys.StringBuilder(); 
                         
                        sb.append("<table border='1'>"); 
                        for (var key in result) 
                        { 
                                var p = result[key]; 

                                sb.append("<tr>"); 
                                sb.append("<td>Name:" + p.Name + "</td>"); 
                                sb.append("<td>Age:" + p.Age + "</td>"); 
                                sb.append("</tr>"); 
                        } 
                        sb.append("</table>"); 

                        $get("result").innerHTML = sb.toString(); 
                } 
                 
                 
                function btnGetDataTable_click()    
                { 
                        Sample.GetDataTable(onGetDataTableSucceeded); 
                } 
                function onGetDataTableSucceeded(result)    
                {                 
                        var sb = new Sys.StringBuilder(); 
                         
                        sb.append("<table border='1'>"); 
                        for (var i=0; i<result.rows.length; i++) 
                        { 
                                sb.append("<tr>"); 
                                sb.append("<td>Name:" + result.rows[i].Name + "</td>"); 
                                sb.append("<td>Age:" + result.rows[i].Age + "</td>"); 
                                sb.append("</tr>"); 
                        } 
                        sb.append("</table>"); 

                        $get("result").innerHTML = sb.toString(); 
                } 
                 
                 
                function btnGetXml_click() 
                { 
                        Sample.GetXml(onGetXmlSucceeded); 
                } 
                function onGetXmlSucceeded(result) 
                { 
                        if (document.all) 
                        { 
                                $get("result").innerText = "\r\n" + result.xml; 
                        } 
                        else 
                        { 
                                // Firefox 
                                $get("result").textContent = "\r\n首节点:" + result.documentElement.nodeName; 
                        } 
                } 
        </script> 

</asp:Content>
 
 
运行结果
1、单击“SimpleTypeTest”按钮
stringTest:abc;boolTest:False;intTest:1;floatTest:1.1;datetimeTest:2007-7-5 0:59:35;enumTest:Sunday

2、单击“GetPerson”按钮
Name:webabcd;Age:27

3、单击“SetPerson”按钮
Name:webabcd;Age:27

4、单击“GetList”按钮
Name:0000 Age:20
Name:0001 Age:21
Name:0002 Age:22
Name:0003 Age:23
Name:0004 Age:24
Name:0005 Age:25
Name:0006 Age:26
Name:0007 Age:27
Name:0008 Age:28
Name:0009 Age:29

5、单击“SetList”按钮
Name:1000 Age:20
Name:1001 Age:21
Name:1002 Age:22
Name:1003 Age:23
Name:1004 Age:24
Name:1005 Age:25
Name:1006 Age:26
Name:1007 Age:27
Name:1008 Age:28
Name:1009 Age:29

6、单击“GetDictionary”按钮
Name:0000 Age:20
Name:0001 Age:21
Name:0002 Age:22
Name:0003 Age:23
Name:0004 Age:24
Name:0005 Age:25
Name:0006 Age:26
Name:0007 Age:27
Name:0008 Age:28
Name:0009 Age:29

7、单击“GetDataTable”按钮
Name:0000 Age:20
Name:0001 Age:21
Name:0002 Age:22
Name:0003 Age:23
Name:0004 Age:24
Name:0005 Age:25
Name:0006 Age:26
Name:0007 Age:27
Name:0008 Age:28
Name:0009 Age:29

8、单击“GetXml”按钮
<?xml version="1.0"?>
<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person><Name>0000</Name><Age>20</Age></Person>
<Person><Name>0001</Name><Age>21</Age></Person>
<Person><Name>0002</Name><Age>22</Age></Person>
<Person><Name>0003</Name><Age>23</Age></Person>
<Person><Name>0004</Name><Age>24</Age></Person>
<Person><Name>0005</Name><Age>25</Age></Person>
<Person><Name>0006</Name><Age>26</Age></Person>
<Person><Name>0007</Name><Age>27</Age></Person>
<Person><Name>0008</Name><Age>28</Age></Person>
<Person><Name>0009</Name><Age>29</Age></Person>
</ArrayOfPerson>


GeneratedProxy.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" Title="Generated Proxy Classes" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"> 
                <services> 
                        <asp:ServiceReference Path="Hello.asmx" /> 
                </services> 
        </asp:ScriptManagerProxy> 
        <p> 
                调用Web Service 
                <br /> 
                var myServiceProxy = MyNameSpace.MyService(); 
                <br /> 
                myServiceProxy.MyServiceMethod(param1, param2, , succeededCallback, failedCallback, 
                userContext); 
        </p> 
        <ul> 
                <li>param - 在前面顺序地写参数 </li> 
                <li>succeededCallback - 调用成功的回调函数 </li> 
                <li>failedCallback - 调用失败的回调函数 </li> 
                <li>userContext - 调用成功的回调函数 </li> 
        </ul> 
        <p> 
                调用成功的回调函数 
                <br /> 
                function succeededCallback(result, userContext, methodName) { } 
        </p> 
        <ul> 
                <li>result - 调用WebService的方法后返回的数据 </li> 
                <li>userContext - 用户上下文 </li> 
                <li>methodName - 调用的方法名 </li> 
        </ul> 
        <p> 
                调用失败的回调函数 
                <br /> 
                function failedCallback(error, userContext, methodName) { } 
        </p> 
        <ul> 
                <li>error - 调用WebService的方法后返回的数据(Sys.Net.WebServiceError对象) </li> 
                <li>userContext - 用户上下文 </li> 
                <li>methodName - 调用的方法名 </li> 
        </ul> 
        <p> 
                <input type="text" id="txtName" value="webabcd" /> 
                <input type="button" id="btnHello" value="Hello" /> 
        </p> 
        <div id="result" /> 

        <script type="text/javascript"> 
                // defaultSucceededCallback - 调用成功的回调函数 
                Hello.set_defaultSucceededCallback(onSucceeded); 
                // defaultFailedCallback - 调用失败的回调函数 
                Hello.set_defaultFailedCallback(onFailed); 
                // defaultUserContext - 用户上下文 
                Hello.set_defaultUserContext("webabcd"); 
                // path - WebService的路径 
                alert(Hello.get_path()); 
                // timeout - 超时时间 
                alert(Hello.get_timeout()); 
         
                function btnHello_onclick()    
                { 
                        var name = $get("txtName").value; 
                        Hello.Say(name); 
                } 

                function onSucceeded(result, context, methodName)    
                { 
                        $get("result").innerHTML = result + "<br />"; 
                        $get("result").innerHTML += "默认的Context:" + context + "<br />"; 
                        $get("result").innerHTML += "调用的方法名:" + methodName + "<br />"; 
                } 
                 
                function onFailed(error)    
                { 
                        $get("result").innerHTML = error.get_message() + "<br />"; 
                } 
        </script> 

</asp:Content>
 
 
运行结果
1、页面加载
弹出框,信息:/Web/WebService/Hello.asmx
弹出框,信息:0

2、单击“Hello”按钮
WebService Hello webabcd
默认的Context:webabcd
调用的方法名:Say


WebServiceProxy.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" Title="WebServiceProxy Class" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
        <p> 
                var webRequest = Sys.Net.WebServiceProxy.invoke(path, methodName, useHttpGet, parameters, 
                succeededCallback, failedCallback, userContext, timeout); 
        </p> 
        <ul> 
                <li>path - WebService的url </li> 
                <li>methodName - 调用的方法名 </li> 
                <li>useHttpGet - 是否使用HTTP GET方式 </li> 
                <li>parameters - 参数(示例:{"param1":196610, "param2":"Hello"}) </li> 
                <li>succeededCallback - 调用成功的回调函数 </li> 
                <li>failedCallback - 调用失败的回调函数 </li> 
                <li>userContext - 用户上下文 </li> 
                <li>timeout - 超时时间 </li> 
                <li>返回值 - 相关的WebRequest对象 </li> 
        </ul> 
        <input type="button" id="btnHello" value="Hello" /> 
        <br /> 
        <br /> 
        <div id="result" /> 

        <script type="text/javascript"> 
                function btnHello_ 
                { 
                        var wRequest =    
                        Sys.Net.WebServiceProxy.invoke 
                        ( 
                                "Hello.asmx", 
                                "Say", 
                                false, 
                                {"name":'webabcd'}, 
                                onSucceeded, 
                                null, 
                                null, 
                                -1 
                        ); 
                         
                        $get("result").innerHTML = wRequest.get_body() + "<br />"; 
                } 
                 
                function onSucceeded(result) 
                { 
                        $get("result").innerHTML += result + "<br />"; 
                } 
        </script> 

</asp:Content>
 
 
运行结果
1、单击“Hello”按钮
{"name":"webabcd"}
WebService Hello webabcd


OK
[源码下载]
 



     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344812,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
.NET数据库编程求索之路--9.使用EF实现
9.使用EF实现   源码下载: /Files/SummerRain/NetDbDevRoad/9使用EF实现1自动ORM简单三层.rar /Files/SummerRain/NetDbDevRoad/9使用EF实现2自动纯POCO代理简单三层.
646 0
.NET数据库编程求索之路--10.使用EF实现(划分输入验证层)
10.使用EF实现(划分输入验证层) 注:使用partial类来实现输入验证逻辑的分层。   源码下载: /Files/SummerRain/NetDbDevRoad/a使用EF实现4划分输入验证层.
566 0
对于已经添加引用,还找不到类型或名字空间的错误及svn客户端清除用户帐号密码
1 已经添加过引用,却找不到类型或名字空间。 可以看下项目的的.net framework版本是否一致。 项目(例如类库项目)右键(vs解决方案资源管理器)——》属性——》应用程序——》目标框架。 也就是检查一下引用项目与被引用项目的目标框架是否版本一致。
661 0
Java网络编程--InetAdress类
一、地址 java.net包中的InetAddress 类对象含有一个Internet主机地址的域名和Ip地址 www.sina.com.cn/202.108.35.210   二、获取地址 1、获取Internet上主机的地址 使用InetAddress类的静态方法 getByName...
600 0
【转】BarTender与ASP.NET的集成小结(条码标签打印编程)
话说自从上次发了篇NHibernate的资料后,好久没有写东西了,半年来一直在忙一个项目,做完项目后,发现很多东西虽然当时做了,懂了,但是很快就会模糊了,于是又再想起总结的重要性~~没啥地方好放资料的,放在博客园也是一个不错的选择~~   本人也是新手,写的不好的地方,请多原谅。
1825 0
Asp.Net Web API 2第三课——.NET客户端调用Web API
原文:Asp.Net Web API 2第三课——.NET客户端调用Web API Asp.Net Web API 导航     Asp.Net Web API第一课——入门http://www.cnblogs.com/aehyok/p/3432158.html       Asp.Net Web API第二课——CRUD操作http://www.cnblogs.com/aehyok/p/3434578.html 前言 本教程演示从一个控制台应用程序,使用HttpClient调用Web API。
1084 0
简单实用SQL脚本Part:查找SQL Server 自增ID值不连续记录
原文:简单实用SQL脚本Part:查找SQL Server 自增ID值不连续记录        在很多的时候,我们会在数据库的表中设置一个字段:ID,这个ID是一个IDENTITY,也就是说这是一个自增ID。
1172 0
.NET数据库编程求索之路--7.使用ADO.NET实现(工厂模式-实现多数据库切换)(4)
7.使用ADO.NET实现(工厂模式-实现多数据库切换)(4) 7.7 业务逻辑层HomeShop.BLL OrderBiz.cs View Code  1 using System; 2 using System.
754 0
13262
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载