HTML網頁調用Web Service的方法

正在準備一個雲盤環境,驗證一些細節,爲了測試方便,我直接用html調用WebService查看是否正確返回結果。相信不少朋友作過這種簡單的事情,因爲不熟悉html寫法加上本身思惟僵化走了點彎路,整理下來給本身備忘。 html

WebService java

@GET
@Path("yyy")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public File getSomthing(
            @QueryParam("sthId1") String bucketName,
            @QueryParam("sthId2") String objectName
    ) {

HTML方式
參見:http://msdn.microsoft.com/en-us/library/45fez2a8%28v=vs.80%29.aspx web

<form method=POST action='http://localhost:8080/xxx/yyy'>
       <input type="text" size="5" name='sthId1'\"></td>
       <input type="text" size="5" name='sthId2'\"></td>
       <input type=submit value="Try"> </td>
   </form>


後邊的代碼未驗證,記錄備忘,參見:
http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr 緩存

調用HelloWorld,無參數 測試

<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> 

調用GetAge,帶三個參數 ui

<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>

返回緩存值調用 url

<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>
相關文章
相關標籤/搜索