Call Web Service through Java Script in the Client

xiaoxiao2021-03-06  73

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

=

"

/ n

"

(Om.es)

==

NULL

?

'': ('SOAP

-

ENV: EncodingStyle

=

"

' Om.ES '

"

'))

SNS

"

> / n

"

Szheader

'

<

SOAP

-

ENV: BODY

>

'

Szparams

"

/ n

"

"

/ 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

.................