[总结]利用Javascript 调用WebService

Calling WebServices using Javascript

If you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. The "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP).

To use the "WebService" behavior, you must attach it to an element using the STYLE attribute, as follows:

<DIV ID="GiveItAName"
STYLE="behavior:url(webservice.htc)"></DIV>

A complete example taken from the Microsoft Web site is as follows:

<html>
<head>
<script language="JavaScript">
var iCallID;

function init()
{
service.useService
("
http://myserver.com/services/myservice.asmx?WSDL",
                   "servicename");
}

function onmyresult()
{
   if ((event.result.error)&&(iCallID==event.result.id))
   {
      var xfaultcode = event.result.errorDetail.code;
      var xfaultstring = event.result.errorDetail.string;
      var xfaultsoap = event.result.errorDetail.raw;

      // Add code to output error information here
      alert("Error ");
   }
   else
   {
      service.innerHTML= "The method returned the result: "
                         + event.result.value;
   }
}
</script>
</HEAD>
<body onload="init();">
<BR>
Enter a Value <input type='text' id='param1'>
<BR>
<button onclick='iCallID = service.servicename.callService
("ProcedureName", param1.value);'>Call A Web Method</button>
<div id="service"
     style="behavior:url(webservice.htc)"
     onresult="onmyresult();">
</div>
</body>
</html>

source: http://weblogs.asp.net/Varad/archive/2004/06/14/155671.aspx


其实这篇更好
Remote Scripting in a .NET World
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting11122001.asp

Scripting Web Services
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting03132000.asp


调用的方法有很多种。但总体来说它是分两类:
一、有返回值的调用法:(该方法有没有反回值都可以用)
< script language="JavaScript" >
function getDatal(url){
var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.4.0");//创建XMLHTTPRequest对象,需MSXML4.0支持 ["MSXML2.XMLHTTP.4.0"、"MSXML2.DOMDocument.4.0"]
xmlhttp.open("GET",url,false,"",""); //使用HTTP GET初始化HTTP请求
xmlhttp.send(""); //发送HTTP请求并获取HTTP响应
return xmlhttp.responseXML; //获取XML文档
}
< /script >


二、没有返回值的调用法:
上面的方法可以用。并且最起码还有其它两种方案:
<script language="JavaScript">
function gotoService()
{
myframe.src="http://../../serverics.asmx?aa";//这样就可以使你的方法被调用一次。
}
</script>
<iframe id="myframe"></iframe>
或者
window.open(服务URI);
或许打开模式对话框

============================
在坛上找了一篇上面的回复,但不是很情楚第一方法的一些细节,请熟悉的朋友针对下面的简单的应用情况给出上面第一种方法的具体参数的设定情况.

现有一web service 工作程:DataTransfer,其中有一个名为"test"的service,web method名为"test1",其返回值为"hello,world!".
现在页面上用javascript调用test.test1方法,显示"hello,world!".

function btnGetData_onclick()
{
var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.4.0");
xmlhttp.open("Get","http://localhost/DataTransfer/test.asmx?wsdl",false,"","");
xmlhttp.send("");
Form1.txtResults.value=xmlhttp.responseText;
}
返回的不是"hello,world!",请修正我的程序!




这里有:
http://chs.gotdotnet.com/quickstart/aspplus/




posted @ 2005-08-17 12:21  PointNet  阅读(7163)  评论(1编辑  收藏  举报