Home / AJAX

Sys.Net.WebServiceProxy invoke Method

RSS
Modified on 2009/11/14 19:12 by Administrator Categorized as Uncategorized

Invokes the specified Web service method.

var webRequest = Sys.Net.WebServiceProxy.invoke(ServicePath, methodName, useGet, params, onSuccess, onFailure, userContext, timeout, enableJsonp, jsonpCallbackParameter);

Parameters

Parameter

Description

ServicePath

A string that contains the Web service URL. ServicePath can be set to a fully qualified URL (http://www.mySite.com/myService.asmx), to an absolute URL without the host name or the fully qualified domain name (FQDN) (/myService.asmx), or to a relative URL (../myService.asmx).

The WebRequest class makes sure that the URL is converted into a form usable by network executors.

methodName

A string that contains the name of the Web service method to invoke.

useGet

(Optional) false to set the Web request HTTP verb to POST; otherwise, true. The default is false.

NoteNote:

The call to the Web service will fail if the verb defined by useGet does not match the ScriptMethodAttribute setting of the corresponding Web server method. For example, the following attribute code (in C#) requires that the call be made with a GET verb:

params

(Optional) A JavaScript dictionary that contains named properties (fields) that correspond to the parameters of the method to call, as in this example:

 
{"param1":196610,"param2":"Hello"}
NoteNote:

The field names in the dictionary must match the names of Web service methods.

If the Web server method does not take any parameters, params can be left out or can be null or an empty dictionary ({}). Any values that you pass are then ignored.

If the dictionary contains key/value pairs that do not correspond to a parameter of the Web server method, they are ignored.

onSuccess

(Optional) The function that is invoked as a callback if the Web service method call returns successfully.

onSuccess can be set to null if you do not need it and if you must specify a value for the remaining parameters.

If no callback function is provided, no action is taken when the Web service method finishes successfully.

onFailure

(Optional) The function that is invoked as a callback if the Web service method call fails.

onFailure can be set to null if you do not need it and if you must specify a value for the remaining parameters.

If no callback function is provided, no action is taken if an error occurs during the Web service method call.

userContext

(Optional) Any user-specific information. userContext can be any JavaScript primitive type, array, or object.

The contents of userContext are passed to the callback functions (if any). If userContext is not provided, null is passed to the callback function.

timeout

(Optional) The time in milliseconds that the network executor must wait before timing out the Web request. timeout can be an integer or null. By defining a time-out interval, you can control the time that the application must wait for the callback to finish.

enableJsonp

(Optional) true to indicate that the service supports JSONP for cross-domain calls; otherwise, false.

jsonpCallbackParameter

(Optional) The callback function name for a JSONP request.

Return Value

The WebRequest instance that is used to call the method. This instance can be used to stop the call.

Remarks

The Web service path is assigned at run time, not through the path attribute of the <asp:ServiceReference> element of the <asp:ScriptManager> control in the page.

When you call the invoke method from JavaScript code, an asynchronous Web request is issued for the Web service method specified in methodName. When the request returns, the appropriate JavaScript callback function (succeeded or failed) is called.

You do not have to pass callback functions or user context as parameters in the method call. Instead, you can specify default callback or user context settings as properties of the generated proxy object. For more information, see Generated Proxy Classes.

Example

The following example shows how to use invoke to call a Web service method. The example shows a Web page, a client script, and a Web service called by the page through the client script.

See Also

Other Resources

Calling Web Services from Client Script in ASP.NET AJAX