博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JS+WebService

Posted on 2010-04-28 16:40  李望生  阅读(273)  评论(2)    收藏  举报
代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title></title>
    <script language="javascript">
    
// <!CDATA[
        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;
            }
        }

        
function createXMLHttpRequest() {
            
if (window.ActiveXObject) {
                xmlhttp 
= new ActiveXObject("Microsoft.XMLHTTP");
            }
            
else if (window.XMLHttpRequst) {
                xmlhttp 
= new XMLHttpRequest();
            }
        }

        
//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;
        }


        
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("admin"), "http://localhost:2692/Web/WebService/WorkFlowService.asmx""http://tempuri.org/");
        }
    
// ]]>
    </script>

</head>
<body>
    
<form id="form1">
    
<input id="Button1" type="button" value="button" onclick="SayHello_onclick()" />
    <label id="h"></label>
    </form>
</body>
</html>