远方蔚蓝
一刹那情真,相逢不如不见

文章数量 126

访问次数 199887

运行天数 1437

最近活跃 2024-10-04 23:36:48

进入后台管理系统

Javascript请求webservice的简单例子


<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>htmlWebpackPlugin.options.title</title>
  </head>
  <body>
  
    <input type="button" value="viewResult" onclick="invokeServerFunction()" />  
	<br/>  
	<br/>  
	Web服务所返回的结果为:<span id="result"></span>  
	<script type="text/javascript">  
    function invokeServerFunction(){  
        var xmlHttp;  
        try{  
            // Firefox, Safari, Opera 8.0+  
            xmlHttp = new XMLHttpRequest();  
        }catch(e){  
            // Internet Explorer  
            try{  
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");  
            }catch(e){  
                try{  
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
                }catch(e){  
                    alert("很遗憾, 您的浏览器不支持AJAX, 请使用其它浏览器, 如Firefox、Safari、Opera8.0+");  
                    return false;  
                }  
            }  
        }  
          
		xmlHttp.onreadystatechange = function(){  
			if(4 == xmlHttp.readyState){  
				alert(xmlHttp.status);  
				if(200 == xmlHttp.status){  
					document.getElementById("result").innerHTML = xmlHttp.responseText;  
				}  
			}  
		}  
		  
		var data = ''
					+ '<?xml version="1.0" encoding="utf-16"?>'
					+ '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'
					+ '<soap:Body>'
					+ '<allDsjk xmlns="http://server.wei.com">'
					+ '<xml>0000</xml>' // 报文参数,这里传入的字符串如果存在XML特定规范符号需要转码,如'<'或'>'
					+ '</allDsjk>'
					+ '</soap:Body>'
					+ '</soap:Envelope>'
		  
		// 请求的webservice地址
		var url = "";  
		  
		xmlHttp.open("POST", url, true);  
		  
		xmlHttp.setRequestHeader ("Content-Type", "text/xml; charset=UTF-8");  
		  
		//这里的第二个参数,是服务端在services.xml中指定的<namespace>加上所要调用的allDsjk()方法名  
		xmlHttp.setRequestHeader ("SOAPAction", "");  
		  
		xmlHttp.send(data);  
	 }  
	</script>  
  </body>
</html>