zoukankan      html  css  js  c++  java
  • JS调用webservice的通用函数。

    调用:

    RequestByPost(method,variable,value,url,_Namespace)

    method:webservice的方法名

    variable:webservice的方法的变量名数组

    value:webservice的方法的变量的值的数组

    url:请求的地址(asmx文件地址)

    _Namespace:webservice的命名空间

    <script language="javascript" type="text/javascript">
    // <!CDATA[

    //define
    var xmlhttp;
    var value=new Array();
    var variable=new Array();

    //Show Response MSG.
    function handleStateChange()
    {
        
    var h=document.getElementById("Label1");
        
    if(xmlhttp.readyState==4)
        
    {
            
    if(xmlhttp.status==200)
            
    {
                alert(xmlhttp.responseText);
                h.innerHTML
    =xmlhttp.responseText;
                
    //h.innerHTML=xmlhttp.responseXML;
            }

            
    else if(xmlhttp.status==404)
            
    {
                h.innerHTML
    ="<br>找不到请求的服务器资源!";
            }

        }

        
    else if(xmlhttp.readyState==0)
        
    {
            h.innerHTML
    ="<br>未初始化!";
        }

        
    else if(xmlhttp.readyState==1)
        
    {
            h.innerHTML
    ="<br>正在加载……!";
        }

        
    else if(xmlhttp.readyState==2)
        
    {
            h.innerHTML
    ="<br>已经加载完成!";
        }

        
    else if(xmlhttp.readyState==3)
        
    {
            h.innerHTML
    ="<br>正在和服务器交互";
        }

        
    else
        
    {
            h.innerHTML
    =xmlhttp.responseXML;
        }

        
    }


    //Get Request Data's length
    function getlen(str)
    {
    var bytesCount=0;
    for (var i = 0; i < str.length; i++)
    {
    var c = str.charAt(i);
    if (/^[\u0000-\u00ff]$/.test(c))   //匹配双字节
          {
    bytesCount 
    += 1;
    }

    else
    {
    bytesCount 
    += 2;
    }

    }

    return bytesCount;

     }
      

        
    //Create XMLHttpRequest Object
      function createXMLHttpRequest()
    {

        
    if(window.ActiveXObject)
        
    {
            xmlhttp
    =new ActiveXObject("Microsoft.XMLHTTP");
        }

        
    else if(window.XMLHttpRequst)
        
    {
            xmlhttp
    =new XMLHttpRequest();
        }

    }
       

    //send Request By HTTP POST

    function RequestByPost(method,variable,value,url,_Namespace)
    {
    createXMLHttpRequest();
    var data;
    data 
    = '<?xml version="1.0" encoding="utf-8"?>'
    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/">'
    data 
    = data + '<soap:Body>'
    data 
    = data + '<'+method+' xmlns="'+_Namespace+'">'
    for(var i=0;i<variable.length;i++)
    {
        data 
    = data + '<'+variable[i]+'>'+value[i]+'</'+variable[i]+'>'

    }

    data 
    = data + '</'+method+'>'
    data 
    = data + '</soap:Body>'
    data 
    = data + '</soap:Envelope>'

    xmlhttp.onreadystatechange
    =handleStateChange;
    xmlhttp.Open(
    "POST",url, true); 
    xmlhttp.SetRequestHeader (
    "Content-Type","text/xml; charset=utf-8"); 
    xmlhttp.SetRequestHeader (
    "Content-Length",getlen(data)); 
    xmlhttp.SetRequestHeader (
    "SOAPAction",_Namespace+method); 
    xmlhttp.Send(data); 
    alert(data);
    }



    //CallHelloWorld!
    function SayHello_onclick() {
    //alert(document.getElementById('YourName').value);
      RequestByPost("HelloWorld",new Array("msg"),new Array(document.getElementById('YourName').value),"WebService.asmx","localhost/");
    }

    //WeatherReport Test:
    function Button2_onclick() {
     RequestByPost(
    "getWeatherbyCityName",new Array("theCityName"),new Array(document.getElementById('CityName').value),"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx","http://WebXml.com.cn/");
    }


    // ]]>
    </script>

    ////////////////////////////////
    ////////Sixi. Let it be.../////
    //////////////////////////////

  • 相关阅读:
    iOS之地理位置及定位系统 -- 入门笔记(用Swift)
    网易新闻iOS版使用的18个开源组件
    自学 iOS – 三十天三十个 Swift 项目
    iOS之UI--富文本总结
    IOS开发--横向流水布局实现
    IOS开发--仿制网易新闻
    Runtime 方法替换 和 动态添加实例方法 结合使用
    写给IOS开发工程师的网页前端入门笔记
    Runtime(动态添加属性)
    const,static,extern简介(重要)
  • 原文地址:https://www.cnblogs.com/luluping/p/1385931.html
Copyright © 2011-2022 走看看