[總結]利用Javascript 調用WebService

來源:互聯網
上載者:User
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/


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.