h5调用webservice请求数据
时间: 2023-10-06 17:13:00 浏览: 123
在H5中调用WebService请求数据,可以使用XMLHttpRequest对象来发送SOAP请求,具体步骤如下:
1. 创建XMLHttpRequest对象
```
var xmlhttp = new XMLHttpRequest();
```
2. 设置请求的方法、URL、以及SOAPAction头信息
```
xmlhttp.open('POST', 'http://example.com/Service.asmx', true);
xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=UTF-8');
xmlhttp.setRequestHeader('SOAPAction', 'http://example.com/Service/GetData');
```
3. 构造SOAP请求体,并发送请求
```
var soapRequest = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body>' +
'<GetData xmlns="http://example.com/Service/">' +
'<param1>value1</param1>' +
'<param2>value2</param2>' +
'</GetData>' +
'</soap:Body>' +
'</soap:Envelope>';
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
// 处理返回的SOAP响应数据
}
};
xmlhttp.send(soapRequest);
```
4. 解析返回的SOAP响应数据
在onreadystatechange事件处理函数中,可以使用XMLHttpRequest对象的responseXML属性获取返回的SOAP响应数据,并解析其中的数据,例如:
```
var response = xmlhttp.responseXML.getElementsByTagName('GetDataResult')[0].textContent;
```
阅读全文
相关推荐












