使用JavaScript調用WebServices服務

來源:互聯網
上載者:User

<%@ page language="java" pageEncoding="UTF-8"%></p><p><script type="text/javascript"><br />function invokeServerFunction(){<br />var xmlHttp;<br />try{<br />// Firefox, Safari, Opera 8.0+<br />xmlHttp = new XMLHttpRequest();<br />}catch(e){<br />// Internet Explorer<br />try{<br />xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");<br />}catch(e){<br />try{<br />xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");<br />}catch(e){<br />alert("很遺憾, 您的瀏覽器不支援AJAX, 請使用其它瀏覽器, 如Firefox、Safari、Opera8.0+");<br />return false;<br />}<br />}<br />}</p><p>xmlHttp.onreadystatechange = function(){<br />if(4 == xmlHttp.readyState){<br />alert(xmlHttp.status);<br />if(200 == xmlHttp.status){<br />//document.writeln("Web服務所返回的結果為:" + xmlHttp.responseText);<br />document.getElementById("result").innerHTML = xmlHttp.responseText;</p><p>}<br />}<br />}</p><p>var data;<br />data = '<?xml version="1.0" encoding="UTF-8"?>';</p><p>//這裡要注意,不建議寫成下面這個樣子,否則會報錯<br />//data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />// xmlns:xsd="http://www.w3.org/2001/XMLSchema"<br />// xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';<br />//最好把這三個東西,寫成一行<br />data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';</p><p>data = data + '<soap:Body>';</p><p>//這裡的<sayHello>標籤,對應的是用戶端所要調用的服務端的方法<br />data = data + '<sayHello xmlns="http://www.jadyer.com/XFireDemo">';</p><p>//這裡的<str>標籤,對應的是傳遞給sayHello()方法的參數<br />data = data + '<str>Jadyer22</str>';</p><p>data = data + '</sayHello>';<br />data = data + '</soap:Body>';<br />data = data + '</soap:Envelope>';</p><p>//這是URL,即訪問的地址,其格式為http://IP:連接埠/服務端項目/services/服務端提供的服務對外開放的名字<br />var url = "http://127.0.0.1:8088/XFire_demo/services/XFireServer";</p><p>xmlHttp.open("POST", url, true);</p><p>xmlHttp.setRequestHeader ("Content-Type","text/xml; charset=UTF-8");</p><p>//這裡的第二個參數,是服務端在services.xml中指定的<namespace>加上所要調用的sayHello()方法名<br />xmlHttp.setRequestHeader ("SOAPAction","http://www.jadyer.com/XFireDemo/sayHello");</p><p>xmlHttp.send(data);<br /> }<br /></script></p><p><input type="button" value="viewResult" onclick="invokeServerFunction()" /><br /><br/><br /><br/><br />Web服務所返回的結果為:<span id="result"></span>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.