• [总结]利用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/




  • 相关阅读:
    Java中四种引用类型
    使用VisualVM查看Java Heap Dump
    Java程序性能分析工具Java VisualVM(Visual GC)—程序员必备利器
    JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
    设计模式2---建造者模式(Builder pattern)
    设计模式1---单例模式(Singleton pattern)
    移动端网络优化
    Http请求的 HttpURLConnection 和 HttpClient
    第四章 Activity和Activity调用栈分析 系统信息与安全机制 性能优化
    Android模块化编程之引用本地的aar
  • 原文地址:https://www.cnblogs.com/goody9807/p/216725.html
Copyright © 2020-2023  润新知