更新:2007 年 11 月
调用指定的 Web 服务方法。
var webRequest = Sys.Net.WebServiceProxy.invoke(path, methodName, useHttpGet, parameters, succeededCallback, failedCallback, userContext, timeout);
参数
参数 |
说明 |
||
|---|---|---|---|
path |
Web 服务 URL。path 可设置为完全限定 URL (http://www.mySite.com/myService.asmx)、没有主机名或完全限定域名 (FQDN) 的绝对 URL (/myService.asmx),或者相对 URL (../myService.asmx)。 WebRequest 类可确保该 URL 转换为可由网络执行器使用的形式。 |
||
methodName |
要调用的 Web 服务方法的名称。 |
||
useHttpGet |
(可选)如果 Web 请求 HTTP 谓词为 POST,则为 false;否则为 true。默认值为 false。
|
||
parameters |
(可选)一个 JavaScript 字典,包含与要调用的方法的参数相对应的命名属性(字段),如下例所示:
如果 Web 服务器方法不接受任何参数,则 parameters 可省略、可为 null,也可为空字典 ({})。这种情况下,传递的任何值都将被忽略。 如果字典包含的键/值对不对应于 Web 服务器方法的参数,则它们也将被忽略。 |
||
succeededCallback |
(可选)在 Web 服务方法调用成功返回时,所调用的回调函数。 如果不需要 succeededCallback,并且必须为其余参数指定值,则该回调函数可设置为 null。 如果未提供任何回调函数,则在 Web 服务方法成功完成后,不会执行任何操作。 |
||
failedCallback |
(可选)在 Web 服务方法调用失败时,所调用的回调函数。 如果不需要 failedCallback,并且必须为其余参数指定值,则该回调函数可设置为 null。 如果未提供任何回调函数,则在 Web 服务方法调用期间出现错误时,不会执行任何操作。 |
||
userContext |
(可选)任何特定于用户的信息。userContext 可以为任何 JavaScript 基元类型、数组或对象。 userContext 的内容(如果有)将传给回调函数。如果未提供 userContext,则 null 将传递给回调函数。 |
||
timeout |
(可选)在 Web 请求超时之前,网络执行器必须等待的时间(以毫秒为单位)。timeout 可以为整数或 null。通过定义超时时间间隔,可以控制应用程序必须等待回调完成的时间。 |
返回值
用于调用该方法的 WebRequest 实例。此实例可用于停止调用。
备注
Web 服务路径是在运行时指定的,而不是通过页中 <asp:ScriptManager> 控件的 <asp:ServiceReference> 元素的 path 属性指定的。
在 JavaScript 代码中调用 invoke 方法时,将为 methodName 中指定的 Web 服务方法发出异步 Web 请求。当该请求返回时,将调用相应的 JavaScript 回调函数(成功或失败)。
不必将回调函数或用户上下文作为方法调用中的参数进行传递。而可以将默认回调或用户上下文设置作为生成的代理对象的属性。有关更多信息,请参见生成的代理类。
示例
下面的示例演示如何使用 invoke 来调用 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">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 8pt Trebuchet MS }
</style>
<title>Web Service Proxy</title>
</head>
<body>
<h2>WebServiceProxy Example</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Scripts>
<asp:ScriptReference Path="WebServiceProxy.js" />
</Scripts>
</asp:ScriptManager>
<table style="font-size:12px">
<tr>
<td>Select Web Service and Method:</td>
<td>
<select id="SelectionId"
onchange="OnSelectMethod(); return false;">
<option value="WebService.asmx" selected>GetServerTime</option>
<option value="WebService.asmx">GetGreetings</option>
<option value="WebService.asmx">PostGreetings</option>
</select>
</td>
</tr>
</table>
<hr />
<!-- Display results. -->
<p>
<span style="background-color:Aqua" id="ResultId"></span>
</p>
</form>
</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">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 8pt Trebuchet MS }
</style>
<title>Web Service Proxy</title>
</head>
<body>
<h2>WebServiceProxy Example</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Scripts>
<asp:ScriptReference Path="WebServiceProxy.js" />
</Scripts>
</asp:ScriptManager>
<table style="font-size:12px">
<tr>
<td>Select Web Service and Method:</td>
<td>
<select id="SelectionId"
onchange="OnSelectMethod(); return false;">
<option value="WebService.asmx" selected>GetServerTime</option>
<option value="WebService.asmx">GetGreetings</option>
<option value="WebService.asmx">PostGreetings</option>
</select>
</td>
</tr>
</table>
<hr />
<!-- Display results. -->
<p>
<span style="background-color:Aqua" id="ResultId"></span>
</p>
</form>
</body>
</html>
var webMethod;
var webServicePath;
// This function shows how to use the
// WebServiceProxy.invoke method without passing
// parameters.
function GetTime()
{
Sys.Net.WebServiceProxy.invoke(webServicePath,
webMethod, false,{}, OnSucceeded,
OnFailed,"User Context",1000000);
}
// This function shows how to use the
// invoke method passing
// parameters and using the GET verb.
// The dictionary field names must match the
// related Web service method parameter names.
function GetGreetings()
{
Sys.Net.WebServiceProxy.invoke(webServicePath,
webMethod, true,
{"greeting":"Have a nice day", "name":" to You (via GET)!"},
OnSucceeded,OnFailed, "User Context",100);
}
// This function shows how to use the
// invoke method passing parameters and using the POST verb.
// The dictionary field names must match the
// related Web service method parameter names.
function PostGreetings()
{
Sys.Net.WebServiceProxy.invoke(webServicePath,
webMethod, false,
{"greeting":"Have a nice day", "name":" to You (via POST)!"},
OnSucceeded,OnFailed, "User Context",100);
}
// This is the callback function invoked
// if the Web service succeeded.
function OnSucceeded(result, eventArgs)
{
// Display the result.
var RsltElem =
document.getElementById("ResultId");
RsltElem.innerHTML = result;
}
// This is the callback function invoked
// if the Web service failed.
function OnFailed(error)
{
// Display the error.
var RsltElem =
document.getElementById("ResultId");
RsltElem.innerHTML =
"Service Error: " + error.get_message();
}
// This function process the user's selection.
function OnSelectMethod()
{
// Get the user's selected method.
var selectionIndex =
document.getElementById("SelectionId").selectedIndex;
webMethod =
document.getElementById("SelectionId").options[selectionIndex].text;
// Get the related Web service path.
webServicePath =
document.getElementById("SelectionId").value;
// Call selected Web service method.
switch (webMethod)
{
case "GetServerTime":
GetTime();
break;
case "GetGreetings":
GetGreetings();
break;
case "PostGreetings":
PostGreetings();
break;
default:
alert("default");
}
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
<%@ WebService Language="C#" Class="Samples.AspNet.WebService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
namespace Samples.AspNet
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string GetGreetings(string greeting,
string name)
{
return greeting + " " + name;
}
[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public string PostGreetings(string greeting,
string name)
{
return greeting + " " + name;
}
[WebMethod]
public string GetServerTime()
{
string serverTime =
String.Format("The current time is {0}.", DateTime.Now);
return serverTime;
}
}
}
说明: