纯js调用webservice接口怎么调用

JavaScript05

纯js调用webservice接口怎么调用,第1张

纯js调用webservice接口举例:

1、HelloWorld.htm (calls Hello World method):

<html>

<head>

<title>Hello World</title>

<script language="JavaScript">

var iCallID

function InitializeService(){

service.useService(http://localhost:1394/MyWebService.asmx?wsdl,

"HelloWorldService")

service.HelloWorldService.callService("HelloWorld")

}

function ShowResult(){

alert(event.result.value)

}

</script>

</head>

<body onload="InitializeService()" id="service"

style="behavior:url(webservice.htc)" onresult="ShowResult()"></body>

</html>

2、GetAge.htm (calls GetAge method, takes 3 parameters):

<html>

<head>

<title>UseSwap</title>

<script language="JavaScript">

function InitializeService(){

service.useService(http://localhost:1394/MyWebService.asmx?wsdl,

"GetAgeService")

}

var StrYear, StrMonth, StrDay

function GetAge(){

StrYear = document.DemoForm.StringYear.value

StrMonth = document.DemoForm.StringMonth.value

StrDay = document.DemoForm.StringDay.value

service.GetAgeService.callService("GetAge", StrYear, StrMonth, StrDay)

}

function ShowResult(){

alert(event.result.value)

}

</script>

</head>

<body onload="InitializeService()" id="service"

style="behavior:url(webservice.htc)" onresult="ShowResult()">

<form name="DemoForm">

Year : <input type="text" name="StringYear"/>

Month : <input type="text" name="StringMonth"/>

Day : <input type="text" name="StringDay"/>

<button onclick="GetAge()">Get Age</button>

</form>

</body>

</html>

3、GetDateTime.htm (returns cached value):

<html>

<head>

<meta http-equiv="refresh" content="2" />

<title>Get Date Time</title>

<script language="JavaScript">

var iCallID

function InitializeService(){

service.useService(http://localhost:1394/MyWebService.asmx?wsdl,

"GetDateTimeService")

service.GetDateTimeService.callService("GetDateTime")

}

function ShowResult(){

alert(event.result.value)

}

</script>

</head>

<body onload="InitializeService()" id="service"

style="behavior:url(webservice.htc)" onresult="ShowResult()">

</body>

</html>

js 主要用于做客户端处理,不与服务器打交道,你这个是与服务器端打交道了,属于特殊情况。你可以用 createobject 调用组件,从而实现与服务端通信。AJAX 、 JS 的ADO 数据库连接、JSON都是这样做的。