学习ajax(四)(从客户端访问WebService)

前端之家收集整理的这篇文章主要介绍了学习ajax(四)(从客户端访问WebService)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

学习ajax(四)(从客户端访问WebService)

服务器端释放WebService方法

名词解释

Ajax技术:异步刷新(javascript)

Ajax.Net:基于asp.net框架是一个ajax框架(不是微软体统的框架)

Asp.net Ajax: 微软提供的框架

编写一个普通的Asp.net webservice

为WebSrvice类添加自定义属性标记

-ScriptServiceAttribute

释放WebService方法

- 访问级别为public

-使用WeMethidAttribute

页面中ScriptManager(Proxy)引入asmx文件

客户端访问WebService

[Namespaces.]ClassName.MethodName

依次传入参数

传入一个方法作为成功后的回调函数

即使没有返回值也会调用回调函数

InlineScript=false 将会调用代理

页面代码


1@H_404_50@<%
@H_404_50@@PageLanguage@H_404_50@="C#"@H_404_50@AutoEventWireup@H_404_50@="true"@H_404_50@CodeFile@H_404_50@="1_WebServiceFoundation.aspx.cs"@H_404_50@Inherits@H_404_50@="_1_WebServiceFoundation"@H_404_50@@H_404_50@%>@H_404_50@
2@H_404_50@
3@H_404_50@@H_404_50@<!@H_404_50@DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"@H_404_50@"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"@H_404_50@>@H_404_50@
4@H_404_50@
5@H_404_50@@H_404_50@<@H_404_50@htmlxmlns@H_404_50@="http://www.w3.org/1999/xhtml"@H_404_50@@H_404_50@>@H_404_50@
6@H_404_50@@H_404_50@<@H_404_50@headrunat@H_404_50@="server"@H_404_50@>@H_404_50@
7@H_404_50@@H_404_50@<@H_404_50@title@H_404_50@>@H_404_50@UntitledPage@H_404_50@</@H_404_50@title@H_404_50@>@H_404_50@
8@H_404_50@@H_404_50@</@H_404_50@head@H_404_50@>@H_404_50@
9@H_404_50@@H_404_50@<@H_404_50@body@H_404_50@>@H_404_50@
10@H_404_50@@H_404_50@<@H_404_50@formid@H_404_50@="form1"@H_404_50@runat@H_404_50@="server"@H_404_50@>@H_404_50@
11@H_404_50@@H_404_50@<@H_404_50@asp:ScriptManagerID@H_404_50@="ScriptManager1"@H_404_50@runat@H_404_50@="server"@H_404_50@ScriptMode@H_404_50@="Debug"@H_404_50@>@H_404_50@
12@H_404_50@@H_404_50@<@H_404_50@Services@H_404_50@>@H_404_50@
13@H_404_50@@H_404_50@<@H_404_50@asp:ServiceReferencePath@H_404_50@="WebServiceFoundation.asmx"@H_404_50@InlineScript@H_404_50@="true"@H_404_50@@H_404_50@/>@H_404_50@
14@H_404_50@@H_404_50@</@H_404_50@Services@H_404_50@>@H_404_50@
15@H_404_50@@H_404_50@</@H_404_50@asp:ScriptManager@H_404_50@>@H_404_50@
16@H_404_50@
17@H_404_50@@H_404_50@<@H_404_50@inputtype@H_404_50@="button"@H_404_50@value@H_404_50@="GetRandom"@H_404_50@onclick@H_404_50@="getRandom()"@H_404_50@@H_404_50@/>@H_404_50@
18@H_404_50@@H_404_50@<@H_404_50@inputtype@H_404_50@="button"@H_404_50@value@H_404_50@="GetRangeRandom"@H_404_50@onclick@H_404_50@="getRandom(50,100)"@H_404_50@@H_404_50@/>@H_404_50@
19@H_404_50@
20@H_404_50@@H_404_50@<@H_404_50@scriptlanguage@H_404_50@="javascript"@H_404_50@type@H_404_50@="text/javascript"@H_404_50@>@H_404_50@
21@H_404_50@functiongetRandom(minValue,maxValue)
22@H_404_50@@H_404_50@{
23@H_404_50@if@H_404_50@(arguments.length@H_404_50@!=@H_404_50@2@H_404_50@)
24@H_404_50@@H_404_50@{
25@H_404_50@Sample.WebServiceFoundation.GetRandom(getRandomSucceeded);
26@H_404_50@}@H_404_50@
27@H_404_50@else@H_404_50@
28@H_404_50@@H_404_50@{
29@H_404_50@Sample.WebServiceFoundation.GetRangeRandom(minValue,maxValue,getRandomSucceeded);
30@H_404_50@}@H_404_50@
31@H_404_50@}@H_404_50@
32@H_404_50@
33@H_404_50@functiongetRandomSucceeded(result)
34@H_404_50@@H_404_50@{
35@H_404_50@alert(result);
36@H_404_50@}@H_404_50@
37@H_404_50@@H_404_50@</@H_404_50@script@H_404_50@>@H_404_50@
38@H_404_50@@H_404_50@</@H_404_50@form@H_404_50@>@H_404_50@
39@H_404_50@@H_404_50@</@H_404_50@body@H_404_50@>@H_404_50@
40@H_404_50@@H_404_50@</@H_404_50@html@H_404_50@>@H_404_50@
41@H_404_50@

对应的的Webservice


1@H_404_50@<%
@H_404_50@@WebServiceLanguage@H_404_50@="C#"@H_404_50@Class@H_404_50@="Sample.WebServiceFoundation"@H_404_50@@H_404_50@%>@H_404_50@
2@H_404_50@
3@H_404_50@using@H_404_50@System;
4@H_404_50@using@H_404_50@System.Web;
5@H_404_50@using@H_404_50@System.Web.Services;
6@H_404_50@using@H_404_50@System.Web.Services.Protocols;
7@H_404_50@using@H_404_50@System.Web.Script.Services;
8@H_404_50@
9@H_404_50@namespace@H_404_50@Sample
10@H_404_50@@H_404_50@{
11@H_404_50@[WebService(Namespace@H_404_50@=@H_404_50@"http://tempuri.org/"@H_404_50@)]
12@H_404_50@[WebServiceBinding(ConformsTo@H_404_50@=@H_404_50@WsiProfiles.BasicProfile1_1)]
13@H_404_50@[ScriptService]
14@H_404_50@public@H_404_50@class@H_404_50@WebServiceFoundation:System.Web.Services.WebService
15@H_404_50@@H_404_50@{
16@H_404_50@[WebMethod]
17@H_404_50@public@H_404_50@int@H_404_50@GetRandom()
18@H_404_50@@H_404_50@{
19@H_404_50@return@H_404_50@new@H_404_50@Random(DateTime.Now.Millisecond).Next();
20@H_404_50@}@H_404_50@
21@H_404_50@
22@H_404_50@[WebMethod]
23@H_404_50@public@H_404_50@int@H_404_50@GetRangeRandom(int@H_404_50@minValue,int@H_404_50@maxValue)
24@H_404_50@@H_404_50@{
25@H_404_50@return@H_404_50@new@H_404_50@Random(DateTime.Now.Millisecond).Next(minValue,maxValue);
26@H_404_50@}@H_404_50@
27@H_404_50@}@H_404_50@
28@H_404_50@}

客户端访问PageMethod

服务器端

只能在aspx页面中定义

只能那个是公开静态方法

使用WebMethodAttribute标记

ScriptManager的EnablePageMethods属性设置为true

客户端

通过PageMethods.MethodName 访问

//得到国际标准时间

DateTime.UtcNow

对应代码2


1@H_404_50@<%
@H_404_50@@PageLanguage@H_404_50@="C#"@H_404_50@AutoEventWireup@H_404_50@="true"@H_404_50@CodeFile@H_404_50@="2_PageMethods.aspx.cs"@H_404_50@Inherits@H_404_50@="_2_PageMethods"@H_404_50@@H_404_50@%>@H_404_50@
2@H_404_50@
3@H_404_50@@H_404_50@<!@H_404_50@DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"@H_404_50@"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"@H_404_50@>@H_404_50@
4@H_404_50@
5@H_404_50@@H_404_50@<@H_404_50@htmlxmlns@H_404_50@="http://www.w3.org/1999/xhtml"@H_404_50@@H_404_50@>@H_404_50@
6@H_404_50@@H_404_50@<@H_404_50@headrunat@H_404_50@="server"@H_404_50@>@H_404_50@
7@H_404_50@@H_404_50@<@H_404_50@title@H_404_50@>@H_404_50@UntitledPage@H_404_50@</@H_404_50@title@H_404_50@>@H_404_50@
8@H_404_50@@H_404_50@</@H_404_50@head@H_404_50@>@H_404_50@
9@H_404_50@@H_404_50@<@H_404_50@body@H_404_50@>@H_404_50@
10@H_404_50@@H_404_50@<@H_404_50@formid@H_404_50@="form1"@H_404_50@runat@H_404_50@="server"@H_404_50@>@H_404_50@
11@H_404_50@@H_404_50@<@H_404_50@asp:ScriptManagerID@H_404_50@="ScriptManager1"@H_404_50@runat@H_404_50@="server"@H_404_50@EnablePageMethods@H_404_50@="true"@H_404_50@@H_404_50@/>@H_404_50@
12@H_404_50@
13@H_404_50@@H_404_50@<@H_404_50@inputtype@H_404_50@="button"@H_404_50@value@H_404_50@="GetCurrentTime"@H_404_50@onclick@H_404_50@="getCurrentTime()"@H_404_50@@H_404_50@/>@H_404_50@
14@H_404_50@
15@H_404_50@@H_404_50@<@H_404_50@scriptlanguage@H_404_50@="javascript"@H_404_50@type@H_404_50@="text/javascript"@H_404_50@>@H_404_50@
16@H_404_50@functiongetCurrentTime()
17@H_404_50@@H_404_50@{
18@H_404_50@PageMethods.GetCurrentTime(getCurrentTimeSucceeded);
19@H_404_50@}@H_404_50@
20@H_404_50@
21@H_404_50@functiongetCurrentTimeSucceeded(result)
22@H_404_50@@H_404_50@{
23@H_404_50@alert(result);
24@H_404_50@}@H_404_50@
25@H_404_50@@H_404_50@</@H_404_50@script@H_404_50@>@H_404_50@
26@H_404_50@@H_404_50@</@H_404_50@form@H_404_50@>@H_404_50@
27@H_404_50@@H_404_50@</@H_404_50@body@H_404_50@>@H_404_50@
28@H_404_50@@H_404_50@</@H_404_50@html@H_404_50@>@H_404_50@
29@H_404_50@

1using@H_404_50@System;
2@H_404_50@using@H_404_50@System.Data;
3@H_404_50@using@H_404_50@System.Configuration;
4@H_404_50@using@H_404_50@System.Collections;
5@H_404_50@using@H_404_50@System.Web;
6@H_404_50@using@H_404_50@System.Web.Security;
7@H_404_50@using@H_404_50@System.Web.UI;
8@H_404_50@using@H_404_50@System.Web.UI.WebControls;
9@H_404_50@using@H_404_50@System.Web.UI.WebControls.WebParts;
10@H_404_50@using@H_404_50@System.Web.UI.HtmlControls;
11@H_404_50@using@H_404_50@System.Web.Services;
12@H_404_50@
13@H_404_50@public@H_404_50@partial@H_404_50@class@H_404_50@_2_PageMethods:System.Web.UI.Page
14@H_404_50@@H_404_50@{
15@H_404_50@protected@H_404_50@void@H_404_50@Page_Load(object@H_404_50@sender,EventArgse)
16@H_404_50@@H_404_50@{
17@H_404_50@
18@H_404_50@}@H_404_50@
19@H_404_50@
20@H_404_50@[WebMethod]
21@H_404_50@public@H_404_50@static@H_404_50@DateTimeGetCurrentTime()
22@H_404_50@@H_404_50@{
23@H_404_50@return@H_404_50@DateTime.UtcNow;
24@H_404_50@}@H_404_50@
25@H_404_50@}@H_404_50@
26@H_404_50@

错误处理

调用时可以提供一个额外的错误回调函数

包括超时和服务器抛出的异常

超时智能设置在WebService对象上

设置在PageMethods对象上

无法在每个MethodCall时指定

Sys.Net.WebServiceError

复杂数据类型基础

公有属性或公有Field会被释放和接受

容器对象

实现IList接口的对象

实现IDictionary接口的对象

Key必须是String


1@H_404_50@<
@H_404_50@scriptlanguage@H_404_50@=@H_404_50@"@H_404_50@javascript@H_404_50@"@H_404_50@type@H_404_50@=@H_404_50@"@H_404_50@text/javascript@H_404_50@"@H_404_50@>@H_404_50@
2@H_404_50@
3@H_404_50@function@H_404_50@doubleSalary()
4@H_404_50@
5@H_404_50@@H_404_50@{
6@H_404_50@
7@H_404_50@//注意怎么传入一个复杂的数据的
8@H_404_50@
9@H_404_50@var@H_404_50@employee@H_404_50@=@H_404_50@new@H_404_50@Object();
10@H_404_50@
11@H_404_50@employee.FirstName@H_404_50@=@H_404_50@@H_404_50@"@H_404_50@Jeffrey@H_404_50@"@H_404_50@;
12@H_404_50@
13@H_404_50@employee.LastName@H_404_50@=@H_404_50@@H_404_50@"@H_404_50@Zhao@H_404_50@"@H_404_50@;
14@H_404_50@
15@H_404_50@employee.Salary@H_404_50@=@H_404_50@@H_404_50@1000@H_404_50@;
16@H_404_50@
17@H_404_50@
18@H_404_50@
19@H_404_50@ComplexType.DoubleSalary(employee,doubleSalarySucceeded);
20@H_404_50@
21@H_404_50@}@H_404_50@
22@H_404_50@
23@H_404_50@
24@H_404_50@
25@H_404_50@function@H_404_50@doubleSalarySucceeded(result)
26@H_404_50@
27@H_404_50@@H_404_50@{
28@H_404_50@
29@H_404_50@var@H_404_50@message@H_404_50@=@H_404_50@String.format(
30@H_404_50@
31@H_404_50@@H_404_50@"@H_404_50@FirstName:{0}\nLastName:{1}\nFullName:{2}\nSalary:{3}@H_404_50@"@H_404_50@,
32@H_404_50@
33@H_404_50@result.FirstName,
34@H_404_50@
35@H_404_50@result.LastName,
36@H_404_50@
37@H_404_50@result.FullName,
38@H_404_50@
39@H_404_50@result.Salary);
40@H_404_50@
41@H_404_50@
42@H_404_50@
43@H_404_50@alert(message);
44@H_404_50@
45@H_404_50@}@H_404_50@
46@H_404_50@
47@H_404_50@

客户端代理使用细节

函数调用完整性

Invoke(ar1,…,argN,onSucceded,onFailed,userContext)

回调函数完整性

onSucceeded(result,userContext,methodName)

onFailed(result,methodName)

WebService级别默认属性

-timeout

-defaultUserContext

-defaultSucceededCallback

-defaultFailedCallback

猜你在找的Ajax相关文章