更新:2007 年 11 月
允许应用程序通过使用 ECMAScript (JavaScript) 异步调用 Web 服务。
命名空间:Sys.Net
**继承:**无
构造函数
名称 |
说明 |
|---|---|
创建生成的代理类的新实例。 |
成员
名称 |
说明 |
|---|---|
调用相应的 Web 服务方法。 |
|
获取或设置生成的代理类及其实例的默认失败回调函数。 |
|
获取或设置生成的代理类及其实例的默认成功回调函数。 |
|
获取或设置生成的代理类及其实例的默认用户上下文。 |
|
获取和设置 Web 服务 URL。 |
|
获取或设置生成的代理类及其实例的超时间隔。 |
|
在 Web 服务方法请求失败后处理错误信息。 |
|
在 Web 服务方法请求成功返回后处理返回的数据。 |
备注
若要调用 Web 服务的方法,应调用生成的 Web 服务代理类的对应方法。生成的代理类执行对 Web 服务方法的 Web 请求。
说明: |
|---|
要使启用 AJAX 的 ASP.NET 应用程序从脚本调用 Web 服务,必须配置该应用程序注册必需的 ASP.NET HTTP 处理程序。有关更多信息,请参见向客户端脚本公开 Web 服务。 |
您调用的 Web 服务必须是 .asmx 文件,并且必须使用 ScriptServiceAttribute 属性限定。从脚本调用的各个方法必须使用 WebMethodAttribute 属性限定。不带此属性的方法不在代理类中公开。有关更多信息,请参见向客户端脚本公开 Web 服务和从客户端脚本调用 Web 服务。
当呈现 ASP.NET 网页时,将自动生成代理类,该代理类派生自 Sys.Net.WebServiceProxy 类。生成的类的 path 属性引用该 Web 服务 URL。
在您的应用程序中,可以调用生成的代理类成员。或者,您可以创建生成的代理类的实例。您可以指定成功回调函数、失败回调函数以及(可选)超时值和用户上下文,作为每个实例的默认属性。然后您可以使用这些代理类实例调用 Web 服务方法。
示例
下面的代码示例演示如何使用 Web 服务生成的代理类。该示例演示了一个网页、一个客户端脚本以及由该网页通过该客户端脚本调用的一个 Web 服务。
<%@ Page Language="VB" %>
<!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 id="Head1" runat="server">
<title>Using Generated Web Service Proxy Class</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 10pt Trebuchet MS; text-align: center }
</style>
</head>
<body>
<h2>Using Generated Web Service Proxy Class</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="UsingProxyClass.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="UsingProxyClass.js" />
</Scripts>
</asp:ScriptManager>
</form>
<center>
<table style="font-size:12px;" >
<tr align="left">
<td class="text">Get Server Object:</td>
<td>
<button id="Button3"
onclick="GetDefaultColor()">Get Default Color</button>
</td>
</tr>
<tr align="left">
<td class="text">Pass Server Object:</td>
<td>
<button id="Button4"
onclick="SetColor()">Set Color</button>
</td>
</tr>
</table>
</center>
<hr />
<!-- Display current color object. -->
<span id="ResultId"></span>
</body>
</html>
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>Using Generated Web Service Proxy Class</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 10pt Trebuchet MS; text-align: center }
</style>
</head>
<body>
<h2>Using Generated Web Service Proxy Class</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="UsingProxyClass.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="UsingProxyClass.js" />
</Scripts>
</asp:ScriptManager>
</form>
<center>
<table style="font-size:12px;" >
<tr align="left">
<td class="text">Get Server Object:</td>
<td>
<button id="Button3"
onclick="GetDefaultColor()">Get Default Color</button>
</td>
</tr>
<tr align="left">
<td class="text">Pass Server Object:</td>
<td>
<button id="Button4"
onclick="SetColor()">Set Color</button>
</td>
</tr>
</table>
</center>
<hr />
<!-- Display current color object. -->
<span id="ResultId"></span>
</body>
</html>
// The Web service default color.
var defaultRgb;
// The proxy class instance.
var proxyInstance;
// The page feedback display element.
var displayResult;
// This function intializes the global variables and
// assigns default values to the generated proxy.
function pageLoad()
{
// Get page feedback display element.
displayResult =
document.getElementById("ResultId");
// Assign default values to the generated proxy.
Samples.AspNet.UsingProxyClass.set_timeout(200);
Samples.AspNet.UsingProxyClass.set_defaultUserContext("Default context");
Samples.AspNet.UsingProxyClass.set_defaultSucceededCallback(SucceededCallback);
Samples.AspNet.UsingProxyClass.set_defaultFailedCallback(FailedCallback);
}
// This function shows how to get
// a server object.
function GetDefaultColor()
{
// Gets the default color obiect.
Samples.AspNet.UsingProxyClass.GetDefaultColor();
}
// This function shows how to instantiate
// the proxy class to assign its default values.
function SetColor()
{
// Instantiate a color object.
var color =
new Samples.AspNet.ColorObject();
// Define a color array (blue).
var colorArray = new Array("00", "00", "FF");
// Assign the new values to the server color object.
color.message = "The new color is Blue";
color.rgb = colorArray;
// Assign default values for the generated proxy using
// a proxy instance.
proxyInstance = new Samples.AspNet.UsingProxyClass();
proxyInstance.set_timeout(1000);
proxyInstance.set_defaultUserContext("New context");
proxyInstance.set_defaultSucceededCallback(SucceededCallback);
proxyInstance.set_defaultFailedCallback(FailedCallback);
// Set the default color object.
proxyInstance.SetColor(color);
}
// Callback function invoked when the call to
// the Web service methods succeeds.
function SucceededCallback(result, userContext, methodName)
{
var message;
switch(methodName)
{
case ("GetDefaultColor"):
case ("SetColor"):
{
// Get the server default color.
message = result.message;
defaultRgb = result.rgb;
// Transform the rgb array into a string.
var serverColor = defaultRgb[0]+ defaultRgb[1] + defaultRgb[2];
// Display the result.
displayResult.style.color = "yellow";
displayResult.style.fontWeight = "bold";
displayResult.style.backgroundColor = "#" + serverColor;
DisplayMessage(message);
break;
}
default:
{
DisplayMessage("Method unknown");
}
}
}
// Callback function invoked when the call to
// the Web service methods fails.
function FailedCallback(error, userContext, methodName)
{
if(error !== null)
{
displayResult.innerHTML = "An error occurred: " +
error.get_message();
}
}
function DisplayMessage(message)
{
if (document.all)
displayResult.innerText = message;
else
// Firefox
displayResult.textContent = message;
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
<%@ WebService Language="C#" Class="Samples.AspNet.UsingProxyClass" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Collections.Generic;
namespace Samples.AspNet
{
// Define the color type to
// exchange with the client.
public class ColorObject
{
public string message;
public string[] rgb;
public ColorObject()
{
this.message = "The default color is Red";
this.rgb = new string[] { "FF", "00", "00" };
}
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class UsingProxyClass :
System.Web.Services.WebService
{
// Note, because the ColorObject is the returned type
// it does not require that you apply
// the attribute [GenerateScriptType(typeof(ColorObject))]
// to the service class to allow client script
// access.
[WebMethod]
public ColorObject GetDefaultColor()
{
// Instantiate the default color object.
ColorObject co = new ColorObject();
return co;
}
[WebMethod]
public ColorObject SetColor(ColorObject color)
{
// Instantiate the color object.
ColorObject co = new ColorObject();
// Assign the passed values.
co.message = color.message;
co.rgb = color.rgb;
return co;
}
}
}
说明: