js调用webservice

采用sop1.1协议发请求获取列车时刻信息,返回xml数据,未对xml解析。
需要设置浏览器跨域访问,google浏览器通过开始--运行chrome.exe --disable-web-security开启跨域访问。其它浏览器不知道怎么开启这个操作。此操作不安全,请慎用。


<html>
	<head>
		<title>列车时刻查询</title>
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
		<script>
			function search(input) {
				var traincode = input.value;
				if(traincode) {
					var xhr = new XMLHttpRequest();
					xhr.open("POST","http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx", true); //true表示异步,send方法执行完,继续向下执行。等服务器返回数据后,再执行回调方法
					xhr.onreadystatechange=function() {
						if(xhr.readyState==4) {
							if(xhr.status==200) {
								alert(xhr.responseText);
								alert('revice');
							}
						}
					}
					
					var myxml = '<?xml version="1.0" encoding="utf-8"?>' 
								+ '<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/">'
								+ '<soap:Body>'
								+ '<getDetailInfoByTrainCode xmlns="http://WebXml.com.cn/">'
								+ '<TrainCode>' + traincode + '</TrainCode>'
								+ '<UserID></UserID>'
								+ '</getDetailInfoByTrainCode>'
								+ '</soap:Body>'
								+ '</soap:Envelope>'; 
								    
					xhr.setRequestHeader("SOAPAction","http://WebXml.com.cn/getDetailInfoByTrainCode");
					xhr.setRequestHeader("Content-Type", "text/xml; charset=utf-8");  
					xhr.send(myxml);
					alert('over');
				}
			}
		</script>
	</head>
	<body>
		请输入车次:<input type="text" name="traincode" onblur="search(this)"/>
	</body>

猜你喜欢

转载自cookieandsession.iteye.com/blog/1958932