我在使用SOAP客戶端庫的應用程序中使用了SOAP Web服務。 如果我在disable-secuity模式下執行我的代碼,Webservice會正常運行。但是,如果我運行我的代碼而不禁用安全模式,我無法運行我的web服務。SOAP webservice錯誤:對預檢請求的響應未通過使用SOAPClient.js的訪問控制檢查'Access-Control-Access-Origin'
控制器,它調用SOAPClient.js
function GetSoapResponse() {
var pl = new SOAPClientParameters();
SOAPClient.invoke(url, "HelloWorld", pl, true, GetSoapResponse_callBack);
}
function GetSoapResponse_callBack(r, soapResponse) {
if (soapResponse.xml) // IE
alert(soapResponse.xml);
else // MOZ
alert((new XMLSerializer()).serializeToString(soapResponse));
}
SOAPClient.js
/*****************************************************************************\
Javascript "SOAP Client" library
\*****************************************************************************/
function SOAPClientParameters() {
\t var _pl = new Array();
\t this.add = function(name, value) {
\t \t _pl[name] = value;
\t \t return this;
\t }
\t this.toXml = function() {
\t \t var xml = "";
\t \t for (var p in _pl) {
\t \t \t switch (typeof (_pl[p])) {
\t \t \t case "string":
\t \t \t case "number":
\t \t \t case "boolean":
\t \t \t case "object":
\t \t \t \t xml += "<" + p + ">" + SOAPClientParameters._serialize(_pl[p])
\t \t \t \t \t \t + "</" + p + ">";
\t \t \t \t break;
\t \t \t default:
\t \t \t \t break;
\t \t \t }
\t \t }
\t \t return xml;
\t }
}
SOAPClientParameters._serialize = function(o) {
\t var s = "";
\t switch (typeof (o)) {
\t case "string":
\t \t s += o.replace(/&/g, "&").replace(/</g, "<").replace(/>/g,
\t \t \t \t ">");
\t \t break;
\t case "number":
\t case "boolean":
\t \t s += o.toString();
\t \t break;
\t case "object":
\t \t // Date
\t \t if (o.constructor.toString().indexOf("function Date()") > -1) {
\t \t \t var year = o.getFullYear().toString();
\t \t \t var month = (o.getMonth() + 1).toString();
\t \t \t month = (month.length == 1) ? "0" + month : month;
\t \t \t var date = o.getDate().toString();
\t \t \t date = (date.length == 1) ? "0" + date : date;
\t \t \t var hours = o.getHours().toString();
\t \t \t hours = (hours.length == 1) ? "0" + hours : hours;
\t \t \t var minutes = o.getMinutes().toString();
\t \t \t minutes = (minutes.length == 1) ? "0" + minutes : minutes;
\t \t \t var seconds = o.getSeconds().toString();
\t \t \t seconds = (seconds.length == 1) ? "0" + seconds : seconds;
\t \t \t var milliseconds = o.getMilliseconds().toString();
\t \t \t var tzminutes = Math.abs(o.getTimezoneOffset());
\t \t \t var tzhours = 0;
\t \t \t while (tzminutes >= 60) {
\t \t \t \t tzhours++;
\t \t \t \t tzminutes -= 60;
\t \t \t }
\t \t \t tzminutes = (tzminutes.toString().length == 1) ? "0"
\t \t \t \t \t + tzminutes.toString() : tzminutes.toString();
\t \t \t tzhours = (tzhours.toString().length == 1) ? "0"
\t \t \t \t \t + tzhours.toString() : tzhours.toString();
\t \t \t var timezone = ((o.getTimezoneOffset() < 0) ? "+" : "-") + tzhours
\t \t \t \t \t + ":" + tzminutes;
\t \t \t s += year + "-" + month + "-" + date + "T" + hours + ":" + minutes
\t \t \t \t \t + ":" + seconds + "." + milliseconds + timezone;
\t \t }
\t \t // Array
\t \t else if (o.constructor.toString().indexOf("function Array()") > -1) {
\t \t \t for (var p in o) {
\t \t \t \t if (!isNaN(p)) // linear array
\t \t \t \t {
\t \t \t \t \t (/function\s+(\w*)\s*\(/ig).exec(o[p].constructor
\t \t \t \t \t \t \t .toString());
\t \t \t \t \t var type = RegExp.$1;
\t \t \t \t \t switch (type) {
\t \t \t \t \t case "":
\t \t \t \t \t \t type = typeof (o[p]);
\t \t \t \t \t case "String":
\t \t \t \t \t \t type = "string";
\t \t \t \t \t \t break;
\t \t \t \t \t case "Number":
\t \t \t \t \t \t type = "int";
\t \t \t \t \t \t break;
\t \t \t \t \t case "Boolean":
\t \t \t \t \t \t type = "bool";
\t \t \t \t \t \t break;
\t \t \t \t \t case "Date":
\t \t \t \t \t \t type = "DateTime";
\t \t \t \t \t \t break;
\t \t \t \t \t }
\t \t \t \t \t s += "<" + type + ">"
\t \t \t \t \t \t \t + SOAPClientParameters._serialize(o[p]) + "</"
\t \t \t \t \t \t \t + type + ">"
\t \t \t \t } else
\t \t \t \t \t // associative array
\t \t \t \t \t s += "<" + p + ">" + SOAPClientParameters._serialize(o[p])
\t \t \t \t \t \t \t + "</" + p + ">"
\t \t \t }
\t \t }
\t \t // Object or custom function
\t \t else
\t \t \t for (var p in o)
\t \t \t \t s += "<" + p + ">" + SOAPClientParameters._serialize(o[p])
\t \t \t \t \t \t + "</" + p + ">";
\t \t break;
\t default:
\t \t break; // throw new Error(500, "SOAPClientParameters: type '" + typeof(o) + "' is not supported");
\t }
\t return s;
}
function SOAPClient() {
}
SOAPClient.username = null;
SOAPClient.password = null;
SOAPClient.invoke = function(url, method, parameters, async, callback) {
\t if (async)
\t \t SOAPClient._loadWsdl(url, method, parameters, async, callback);
\t else
\t \t return SOAPClient._loadWsdl(url, method, parameters, async, callback);
}
// private: wsdl cache
SOAPClient_cacheWsdl = new Array();
// private: invoke async
SOAPClient._loadWsdl = function(url, method, parameters, async, callback) {
\t // load from cache?
\t debugger;
\t var wsdl = SOAPClient_cacheWsdl[url];
\t if (wsdl + "" != "" && wsdl + "" != "undefined")
\t \t return SOAPClient._sendSoapRequest(url, method, parameters, async,
\t \t \t \t callback, wsdl);
\t // get wsdl
\t var xmlHttp = SOAPClient._getXmlHttp();
\t xmlHttp.open("GET", url + "?wsdl", async);
\t xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
\t xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
\t xmlHttp.setRequestHeader('X-CSRF-Token',"Fetch");
\t xmlHttp.setRequestHeader('Access-Control-Allow-Methods','POST, GET, OPTIONS, PUT, DELETE');
\t xmlHttp.setRequestHeader('Access-Control-Allow-Origin', '*');
\t xmlHttp.setRequestHeader('Access-Control-Allow-Headers',"Origin, X-Requested-With, Content-Type, Accept");
\t
\t if (async) {
\t \t xmlHttp.onreadystatechange = function() {
\t \t \t if (xmlHttp.readyState == 4)
\t \t \t \t SOAPClient._onLoadWsdl(url, method, parameters, async,
\t \t \t \t \t \t callback, xmlHttp);
\t \t }
\t }
\t xmlHttp.send(null);
\t if (!async)
\t \t return SOAPClient._onLoadWsdl(url, method, parameters, async, callback,
\t \t \t \t xmlHttp);
}
SOAPClient._onLoadWsdl = function(url, method, parameters, async, callback, req) {
\t var wsdl = req.responseXML;
\t SOAPClient_cacheWsdl[url] = wsdl; // save a copy in cache
\t return SOAPClient._sendSoapRequest(url, method, parameters, async,
\t \t \t callback, wsdl);
}
SOAPClient._sendSoapRequest = function(url, method, parameters, async,
\t \t callback, wsdl) {
\t // get namespace
\t var ns = (wsdl.documentElement.attributes["targetNamespace"] + "" == "undefined") ? wsdl.documentElement.attributes
\t \t \t .getNamedItem("targetNamespace").nodeValue
\t \t \t : wsdl.documentElement.attributes["targetNamespace"].value;
\t // build SOAP request
\t var sr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope "
\t \t \t + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
\t \t \t + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
\t \t \t + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
\t \t \t + "<soap:Body>" + "<" + method + " xmlns=\"" + ns + "\">"
\t \t \t + parameters.toXml() + "</" + method
\t \t \t + "></soap:Body></soap:Envelope>";
\t // send request
\t var xmlHttp = SOAPClient._getXmlHttp();
\t if (SOAPClient.userName && SOAPClient.password) {
\t \t xmlHttp.open("POST", url, async, SOAPClient.userName,
\t \t \t \t SOAPClient.password);
\t \t \t \t xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
\t xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
\t xmlHttp.setRequestHeader('X-CSRF-Token',"Fetch");
\t xmlHttp.setRequestHeader('Access-Control-Allow-Methods','POST, GET, OPTIONS, PUT, DELETE');
\t xmlHttp.setRequestHeader('Access-Control-Allow-Origin', '*');
\t xmlHttp.setRequestHeader('Access-Control-Allow-Headers',"Origin, X-Requested-With, Content-Type, Accept");
\t \t // Some WS implementations (i.e. BEA WebLogic Server 10.0 JAX-WS) don't support Challenge/Response HTTP BASIC, so we send authorization headers in the first request
\t \t xmlHttp.setRequestHeader("Authorization", "Basic "
\t \t \t \t + SOAPClient._toBase64(SOAPClient.userName + ":"
\t \t \t \t \t \t + SOAPClient.password));
\t } else
\t \t xmlHttp.open("POST", url, async);
\t \t xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
\t xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
\t xmlHttp.setRequestHeader('X-CSRF-Token',"Fetch");
\t xmlHttp.setRequestHeader('Access-Control-Allow-Methods','POST, GET, OPTIONS, PUT, DELETE');
\t xmlHttp.setRequestHeader('Access-Control-Allow-Origin', '*');
\t xmlHttp.setRequestHeader('Access-Control-Allow-Headers',"Origin, X-Requested-With, Content-Type, Accept");
\t var soapaction = ((ns.lastIndexOf("/") != ns.length - 1) ? ns + "/" : ns)
\t \t \t + method;
\t xmlHttp.setRequestHeader("SOAPAction", soapaction);
\t xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
\t if (async) {
\t \t xmlHttp.onreadystatechange = function() {
\t \t \t if (xmlHttp.readyState == 4)
\t \t \t \t SOAPClient._onSendSoapRequest(method, async, callback, wsdl,
\t \t \t \t \t \t xmlHttp);
\t \t }
\t }
\t xmlHttp.send(sr);
\t if (!async)
\t \t return SOAPClient._onSendSoapRequest(method, async, callback, wsdl,
\t \t \t \t xmlHttp);
}
SOAPClient._onSendSoapRequest = function(method, async, callback, wsdl, req) {
\t var o = null;
\t var nd = SOAPClient._getElementsByTagName(req.responseXML, method
\t \t \t + "Result");
\t if (nd.length == 0)
\t \t nd = SOAPClient._getElementsByTagName(req.responseXML, "return"); // PHP web Service?
\t if (nd.length == 0) {
\t \t if (req.responseXML.getElementsByTagName("faultcode").length > 0) {
\t \t \t if (async || callback)
\t \t \t \t o = new Error(
\t \t \t \t \t \t 500,
\t \t \t \t \t \t req.responseXML.getElementsByTagName("faultstring")[0].childNodes[0].nodeValue);
\t \t \t else
\t \t \t \t throw new Error(
\t \t \t \t \t \t 500,
\t \t \t \t \t \t req.responseXML.getElementsByTagName("faultstring")[0].childNodes[0].nodeValue);
\t \t }
\t } else
\t \t o = SOAPClient._soapresult2object(nd[0], wsdl);
\t if (callback)
\t \t callback(o, req.responseXML);
\t if (!async)
\t \t return o;
}
SOAPClient._soapresult2object = function(node, wsdl) {
\t var wsdlTypes = SOAPClient._getTypesFromWsdl(wsdl);
\t return SOAPClient._node2object(node, wsdlTypes);
}
SOAPClient._node2object = function(node, wsdlTypes) {
\t // null node
\t if (node == null)
\t \t return null;
\t // text node
\t if (node.nodeType == 3 || node.nodeType == 4)
\t \t return SOAPClient._extractValue(node, wsdlTypes);
\t // leaf node
\t if (node.childNodes.length == 1
\t \t \t && (node.childNodes[0].nodeType == 3 || node.childNodes[0].nodeType == 4))
\t \t return SOAPClient._node2object(node.childNodes[0], wsdlTypes);
\t var isarray = SOAPClient._getTypeFromWsdl(node.nodeName, wsdlTypes)
\t \t \t .toLowerCase().indexOf("arrayof") != -1;
\t // object node
\t if (!isarray) {
\t \t var obj = null;
\t \t if (node.hasChildNodes())
\t \t \t obj = new Object();
\t \t for (var i = 0; i < node.childNodes.length; i++) {
\t \t \t var p = SOAPClient._node2object(node.childNodes[i], wsdlTypes);
\t \t \t obj[node.childNodes[i].nodeName] = p;
\t \t }
\t \t return obj;
\t }
\t // list node
\t else {
\t \t // create node ref
\t \t var l = new Array();
\t \t for (var i = 0; i < node.childNodes.length; i++)
\t \t \t l[l.length] = SOAPClient
\t \t \t \t \t ._node2object(node.childNodes[i], wsdlTypes);
\t \t return l;
\t }
\t return null;
}
SOAPClient._extractValue = function(node, wsdlTypes) {
\t var value = node.nodeValue;
\t switch (SOAPClient._getTypeFromWsdl(node.parentNode.nodeName, wsdlTypes)
\t \t \t .toLowerCase()) {
\t default:
\t case "s:string":
\t \t return (value != null) ? value + "" : "";
\t case "s:boolean":
\t \t return value + "" == "true";
\t case "s:int":
\t case "s:long":
\t \t return (value != null) ? parseInt(value + "", 10) : 0;
\t case "s:double":
\t \t return (value != null) ? parseFloat(value + "") : 0;
\t case "s:datetime":
\t \t if (value == null)
\t \t \t return null;
\t \t else {
\t \t \t value = value + "";
\t \t \t value = value.substring(0,
\t \t \t \t \t (value.lastIndexOf(".") == -1 ? value.length : value
\t \t \t \t \t \t \t .lastIndexOf(".")));
\t \t \t value = value.replace(/T/gi, " ");
\t \t \t value = value.replace(/-/gi, "/");
\t \t \t var d = new Date();
\t \t \t d.setTime(Date.parse(value));
\t \t \t return d;
\t \t }
\t }
}
SOAPClient._getTypesFromWsdl = function(wsdl) {
\t var wsdlTypes = new Array();
\t // IE
\t var ell = wsdl.getElementsByTagName("s:element");
\t var useNamedItem = true;
\t // MOZ
\t if (ell.length == 0) {
\t \t ell = wsdl.getElementsByTagName("element");
\t \t useNamedItem = false;
\t }
\t for (var i = 0; i < ell.length; i++) {
\t \t if (useNamedItem) {
\t \t \t if (ell[i].attributes.getNamedItem("name") != null
\t \t \t \t \t && ell[i].attributes.getNamedItem("type") != null)
\t \t \t \t wsdlTypes[ell[i].attributes.getNamedItem("name").nodeValue] = ell[i].attributes
\t \t \t \t \t \t .getNamedItem("type").nodeValue;
\t \t } else {
\t \t \t if (ell[i].attributes["name"] != null
\t \t \t \t \t && ell[i].attributes["type"] != null)
\t \t \t \t wsdlTypes[ell[i].attributes["name"].value] = ell[i].attributes["type"].value;
\t \t }
\t }
\t return wsdlTypes;
}
SOAPClient._getTypeFromWsdl = function(elementname, wsdlTypes) {
\t var type = wsdlTypes[elementname] + "";
\t return (type == "undefined") ? "" : type;
}
// private: utils
SOAPClient._getElementsByTagName = function(document, tagName) {
\t try {
\t \t // trying to get node omitting any namespaces (latest versions of MSXML.XMLDocument)
\t \t return document.selectNodes(".//*[local-name()=\"" + tagName + "\"]");
\t } catch (ex) {
\t }
\t // old XML parser support
\t return document.getElementsByTagName(tagName);
}
// private: xmlhttp factory
SOAPClient._getXmlHttp = function() {
\t try {
\t \t if (window.XMLHttpRequest) {
\t \t \t var req = new XMLHttpRequest();
\t \t \t // some versions of Moz do not support the readyState property and the onreadystate event so we patch it!
\t \t \t if (req.readyState == null) {
\t \t \t \t req.readyState = 1;
\t \t \t \t req.addEventListener("load", function() {
\t \t \t \t \t req.readyState = 4;
\t \t \t \t \t if (typeof req.onreadystatechange == "function")
\t \t \t \t \t \t req.onreadystatechange();
\t \t \t \t }, false);
\t \t \t }
\t \t \t return req;
\t \t }
\t \t if (window.ActiveXObject)
\t \t \t return new ActiveXObject(SOAPClient._getXmlHttpProgID());
\t } catch (ex) {
\t }
\t throw new Error("Your browser does not support XmlHttp objects");
}
SOAPClient._getXmlHttpProgID = function() {
\t if (SOAPClient._getXmlHttpProgID.progid)
\t \t return SOAPClient._getXmlHttpProgID.progid;
\t var progids = [ "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0",
\t \t \t "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ];
\t var o;
\t for (var i = 0; i < progids.length; i++) {
\t \t try {
\t \t \t o = new ActiveXObject(progids[i]);
\t \t \t return SOAPClient._getXmlHttpProgID.progid = progids[i];
\t \t } catch (ex) {
\t \t }
\t \t ;
\t }
\t throw new Error("Could not find an installed XML parser");
}
SOAPClient._toBase64 = function(input) {
\t var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/=";
\t var output = "";
\t var chr1, chr2, chr3;
\t var enc1, enc2, enc3, enc4;
\t var i = 0;
\t do {
\t \t chr1 = input.charCodeAt(i++);
\t \t chr2 = input.charCodeAt(i++);
\t \t chr3 = input.charCodeAt(i++);
\t \t enc1 = chr1 >> 2;
\t \t enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
\t \t enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
\t \t enc4 = chr3 & 63;
\t \t if (isNaN(chr2)) {
\t \t \t enc3 = enc4 = 64;
\t \t } else if (isNaN(chr3)) {
\t \t \t enc4 = 64;
\t \t }
\t \t output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
\t \t \t \t + keyStr.charAt(enc3) + keyStr.charAt(enc4);
\t } while (i < input.length);
\t return output;
}
我得到的弗洛翼錯誤:
ERRORS:
OPTIONS:mywebserviceURL.403(禁止)
的XMLHttpRequest不能加載mywebserviceURL.Response預檢請求未通過訪問控制檢查:'接取-控制-Access-Origin'在請求的資源上。因此Origin域不允許訪問。該響應具有http狀態碼403.
未捕獲的NetworkError:未能在'XMLHttpRequest'上執行'發送'。無法加載mywebserviceURL。
我試過的東西。
我試着設置不同的標題:
•「訪問控制請求法」,」 GET,POST,PUT,DELETE,OPTIONS」。
•「訪問控制允許來源」,」 *」
•‘X-要求,用’,」 XMLHttpRequest的」
•‘內容類型’,「文本/ XML;字符集UTF-8」
•‘訪問控制 - Requesst標頭’,」 X-定製頭」
如何調用Web服務?在這裏複製程序 –
@Ankushsoni我已添加代碼 –