From: http://www.cnblogs.com/roger/ The following code implements calling Web Service with Java Script, by calling the web service: TimeService, the server is currently displayed at the client current time, And automatically refreshed with intervals in 1 second.
TimeService: gettime ()
//
Return Time On Server
[WebMethod]
public
String
GetTime ()
{Return DateTime.now.toString ();
The page displayed by the client is WebForm1:
<
HTML
>
<
HEAD
>
<
Title
>
Webform1
Title
>
<
Meta
Name
= "Generator"
Content
= "Microsoft Visual Studio .NET 7.1"
>
<
Meta
Name
= "Code_Language"
Content
= "C #"
>
<
Meta
Name
= "VS_DEFAULTCLIENTScript"
Content
= "JavaScript"
>
<
Meta
Name
= "vs_targetschema"
Content
= "http://schemas.microsoft.com/intellisense/ie5"
>
<
Script
Language
= "JavaScript"
>
Function Timer ("http: //localhost/timeWebservice/timevice.asmx? WSDL", "TimeService"); Service.TimeService.CallService (Callback, "GetTime"); setTimeout ("Timer ()" , 1000);} function callback (res) {if (! Res. error) Time.innertext = Res. Value}
Script
>
HEAD
>
<
Body
MS_Positioning
= "GridLayout"
Online
= "TIMER ()"
>
<
Div
id
= "Service"
Style
= "Behavior: URL (WebService.htc)"
>
Div
>
<
FORM
id
= "Form1"
Method
= "POST"
Runat
= "Server"
>
<
TABLE
id
= "Table1"
Style
= "Z-Index: 101; Left: 8px; Width: 440px; Position: absolute; top: 8px; height: 50px"
Cellspacing = "1"
Cellpadding
= "1"
Width
= "440"
Border
= "0"
>
<
TR
>
<
TD
Style
= "Width: 83px"
>
<
ASP: Label
id
= "Label1"
Runat
= "Server"
>
Current Time:
ASP: Label
>
TD
>
<
TD
> <
Span
id
= "TIME"
>
Span
>
TD
>
TR
>
TABLE
>
FORM
>
Body
>
HTML
>
This is introduced into a file called WebService.htc, which contains two methods that use Java Script to call the Web Service:
UseService and
CallService. WBService.htc can be downloaded by the following address:
Http://msdn.microsoft.com/workshop/author/webservice/webservice.htc
In fact, web service is essentially HTTP, XML, so you can fully call Web Service, interested, you can refer to the _invoke () function in WebService.htc:
VAR
Szaction
=
Om.soapaction;
IF
(Szaction)
! =
NULL
&&&&
Szaction.length
>
0
) Oxmlhttp.xmlhttp.setRequestHeader (
"
SOAPACTION
"
'
"
' szaction '
"
'); Oxmlhttp.xmlhttp.setrEquestHeader
"
Content-Type
"
,
"
Text / XML
"
);
VAR
SNS
=
'XMLNS
=
""
"
for
(
VAR
ns
in
Os.ns) {
VAR
nsuri
=
Os.ns [ns];
IF
(NS)
==
""
||
nsuri
==
""
)
Continue
; SNS
=
"
XMLNS:
"
ns
'
=
"
' Nsuri '
"
';}
VAR
Szheader
=
EncodeHeader (OS, OM, OCALL);
VAR
Szpayload
=
"
XML Version = '1.0'?> / n " (Om.es) == NULL ? '': ('SOAP - ENV: EncodingStyle = " ' Om.ES ' " ')) SNS " > / n " Szheader ' < SOAP - ENV: BODY > ' Szparams " Soap-env: body> / n " " Soap-env: envelope> / N " ; IF (Co.async) {OCALL.OXMLHTTP = OXMLHTTP; OXMLHTTP.XMLHTTTP.ONREADYSTATECHANGE = FUNCTION () {GetResult (OCALL); Try {Oxmlhttp.xmlhttp.send (szpayload); Catch (e) { Return POSTERROR (OCALL, 5 } Return Ocall.id;} Try {Oxmlhttp.xmlhttp.send (szpayload); Catch (e) { Return Returnerror (OCALL, 5 } IF (oxmlhttp.xmlhttp.responsexml.parseerror.errorcode ! = 0 ) {_ERRUNKNOWNS.RAW = OXMLHTTP.XMLHTTP.RESPONSETEXT; Return Returnerror (OCALL, 4 } VAR R; Try {R = ProcessResult (OCALL, OXMLHTTP.XMLHTTP.RESPONSEXML.Documentelement); Catch (e) { Return Returnerror (OCALL, Seduce } Return R; The following is a detailed description of the UserService and the CallService method: UseService Method ESTABLISHES A Friendly Name for a Web Service URL, Which Can The Be Reference from Script. Syntax SELEMENTID.USESERVICE (SWEBServiceURL, SFRIENDLYNAME [, OUSEOPTIONS]) Parameters Selementid Required. Thae id Of the element to which the WebService behavior is attached. sWebServiceURL Required. String specifying the URL of the Web Service, using one of the following path types. See the examples section, where several variations of this parameter are shown. Web Service file name A Web service file, which has an .asmx file extension. This short form of the URL is sufficient, provided that the Web service is located in the same folder as the Web page using the WebService behavior. In this case, the? WSDL query string is assumed by the behavior. WSDL file name A Web Services Description Language (WSDL) file name. The WSDL file must have a .wsdl file extension. Full file path Full path to a WebService (.asmx) or WSDL (.wsdl) File. A File Path TO A Web Service Must Include To? WSDL Query String. Either A Local File Path OR A URL CAN Be Specified. Relative Path A Relative Path To a WebService (.asmx) or WSDL (.wsdl) ) file. A file path to a Web Service must include the? WSDL query string. sFriendlyName Required. String representing a friendly name for the Web Service URL. oUseOptions Optional. An instance of theuseOptions Object. Return Value No Return Value. Remarks After Using this Method, The Identifier Specified In SFRIENDLYNAME CAN BE Used In Script As a reference to the Web Service Specified by SwebserviceURL. The useOptions object can be used when it is necessary to retain the Secure Sockets Layer (SSL) authentication for multiple remote method invocations. For code samples illustrating the use of this technique, see the CreateUseOptions Method. Examples ................. Function init () { Service.useService ("Math.asmx? WSDL", "MyMath"); } script>
div>
body> The Following Examples Illustrate Various Valid Forms of The SwebserviceURL Parameter.
.
Service.useservice ("Math.asmx", "MyMath");
THE FOLLOWING CODE SNIPPET ILLUSTRATES How A WSDL File Can Be Specified for The SwebserviceURL Parameter. In this case......................
Service.useService ("Conversion.wsdl", "MyConverter");
The Following Code Snippet Defines The SWEBSERVICEURL Parameter AS A LOCAL FILE. In this case the? WSDL Query String Must Be Included.
Service.useService ("c: /inetpub/wwwroot/myproject/myws.asmx? WSDL", "MyMath");
The Following Code Snippet Defines The Swebserviceurl Parameter As The Full Http File path to the myws.asmx web service file. In this case the? WSDL Query String Must Be Included.
Service.useservice ("http://localhost/myproject/myws.asmx? wsdl", "mymath");
The following code snippet defines the sWebServiceURL parameter as a relative file path to the myws.asmx Web service file. In this case the? WSDL query string must be included. This example points to a Web Service file two levels up from the Web page.
Service.useservice ("../../ myws.asmx? wsdl", "mymath");
The following code snippet defines the sWebServiceURL parameter as a relative file path to the myws.asmx Web service file. In this case the? WSDL query string must be included. The path points to the myws.asmx Web Service, which is located in the WSFLD Subfolder of The Web Page.
Service.useservice ("./ WSFLD / MYWS.ASMX? WSDL", "MyMath"); CallService Method
Invokes a Method That Is Implement ON A Web Service.
Syntax
Icallid = selementid.sfriendlyname.callservice ([OCallHandler], Fo, Oparam)
Parameters
Selementid Required. Thae
id
Of the element to which the
WebService
Behavior is attached. The friendly name for the Web service, Which is defined by calling there
Useservice
............................ ..
Strfuncname a string representing the name of the remote function being called. The string must be bounded by single or double quotation marks. Objcall A
Call
Object, Which Has The Necessary Properties Defined to Call A Remote Function. Oparam Required. One or More Comma-Delimited Parameters, Which Are Passed To The Method Name Specified by fo.
Return Value
In The Case of Asynchronous Communication, Returns An INTEGER, WHICH IS A UNIQUE Identifier for the instance of the method Call. In the case of synchronous communication, returns a result object.
Remarks
Using this Method Causes The Webservice Behavior To Invoke A Remote Method Call on a web service.
When the callservice method invokes asynchronous communication between the Webservice Behavior and The Web Service by Setting The Web Service by Setting T
Async
Property to True, The Return Value of the Method Is An Integer That Identifies The Instance of The Method Call. in this case, an
ONRESULT
Event Handler or Callback Function Should Be Used to Process The Returned Results.
When the callService method invokes synchronous communication between the WebService behavior and the Web Service by setting the async property to false, the return value of the method is a result object.For an example of using both synchronous and asynchronous communication between the WebService behavior and the Web Service, See the Examples on The
Call
Object Page.
IF OCAllHandler Is Not Specified, The An OnResult Event Handler IS Used To Examine The Results of The CallService. Using this Approach, The Properties of THE
Event
Object Can Be Examined to Determine if The Call WAS Made SuccessFully.
If oCallHandler is specified, then a callback handler function is used to process the results of the method call. The result object is passed to the first parameter of the callback function, so the user specifies the name of the object in the script code. Code Samples Using Each Approach Are Given in
Using the Webservice Behavior
.
Regardless of the type of function used to process the results, the properties exposed to script are similar. If an event handler is used, the syntax used to access result information is event.result. If the callback approach is used, the object name is The name of the first parameter in The Callback Function Declaration. for a Comprehensive List of Property Objects Returned by a Webservice Behavior Call, See
ONRESULT
When passing an
XMLELEMENT
Object to a Web Service Using
Oparam, The Extensible Markup Language (XML) Contained in The
Xmlelement Must Contain A Single Root Node. The root node is not returned by the
Result
Object. in The Following XML Data Island,
Booklist is the root.
Show esample
XML Version = "1.0"?>
16-041 code>
Book>
16-048 code>
Book>
Booklist>
Xml>
The Following Example Shows A Call To a Web Service. The element where the weight. The element where the weight.....................
Var x = myxmlelement.documentelement; // Get the Xmlelement Object
AAA.TST.CALLSERVICE (MyCallbackFunction, "XMlecho", X);
THIS Callback Function Returns The First Child of the Booklist Element by asking for the ZERO Element Child. There Is No Way to ask for the root or booklist element as it has no parent element.
Function mycallbackfunction (res) {
IF (! res. error) {
VAR x = res. value;
VAR Y = x.selectNodes ("BookList") [0] .xml;
Alert (Y);
} else {
Alert (res. errordetail.string);
}
}
EXAMPLE
The Following Example Shows How A Method named cales.
Var iCallID;
Function init ()
{
Service.useservice ("/ services / math.asmx? wsdl", "mymath");
Icallid = Service.Mymath.callService ("add", 5, 6);
}
script>
div>
body>