上一篇介绍了SOAP体的传递部分 其实距离整个SOAP体还有一个 限制很大的头信息 这个头信息也是SOAP体的一部分
这个头信息算得上是SOAP协议标签 如果没有那么无论你传递的XML多么符合SOAP的标准 没有这个头信息 一样是相当于一个普通的HTTP协议
并不是传递WebService 我试过直接将 XML传递 到WebService 但是返回来的是一个错误 no SOAPAction header! 很直观 没有SOAPAction头
这个头的标准值是 命名空间+方法名 但其实你传递“”也是可行的 但是无论这个值是什么 这个头是需要定义的
经过长时间的查找 我终于找到了一个类 XmlHttpRequest 用这个来传递SOAP体并且修改头信息 好了 废话半天了 上代码
function getXmlHttp() {
var xmlHttp;
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttp = new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
return xmlHttp;
}
function webservice(url, options) {
if (typeof (url) == 'object') { //将url写在options里的情况
options = url;
url = url.url;
}
if (!url) return;
if (options.dataType.toLowerCase() == 'json') { //请求JSON格式的数据时,url后面需要加上“/方法名”
url = url + '/' + options.method;
}
var xmlHttp = getXmlHttp(); //获取XMLHttpRequest对象
xmlHttp.open('POST', url, true); //异步请求数据
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
try {
if (xmlHttp.status == 200 && typeof (options.success) == 'function') {
options.success(xmlHttp.responseText);
}
else if ((xmlHttp.status / 100 == 4 || xmlHttp.status / 100 == 5) && typeof (options.error) == 'function') {
options.error(xmlHttp.responseText, xmlHttp.status);
}
else if (xmlHttp.status / 100 == 200 && typeof (options.complete) == 'function') {
options.complete(xmlHttp.responseText, xmlHttp.status);
}
else if (typeof (options.failed) == 'function') {
options.failed(xmlHttp.responseText, xmlHttp.status);
}
}
catch (e) {
}
}
}
xmlHttp.setRequestHeader('Content-Type', options.contentType); //设置请求头的ContentType
// xmlHttp.setRequestHeader('SOAPAction',options.namespace + options.method);//设置SOAPAction
xmlHttp.setRequestHeader('SOAPAction', '""'); //设置SOAPAction
xmlHttp.send(options.data); //发送参数数据
}
function callWbs(){
var data = '<?xml version="1.0" encoding="utf-8"?>'
+' <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>'
+' <method xmlns="Namespace">'
+' <data>value</data>'
+' </method>'
+' </soap:Body>'
+'</soap:Envelope>';
var options = {
namespace: 'Namespace',
method: 'method',
contentType: 'text/xml; charset=utf-8',
dataType: 'xml',
data: data,
success: function (msg) {
alert(msg);
}
};
webservice('http://localhost:端口号/XXX/services/XXX', options);
}
这个callWbs 直接alert出返回的结果
但是这个方法 没办法跨域 等找到XmlHttpRequest类的跨域问题 我再往下写