Dúvida JQuery post ajax WebService

0 respostas
Flasoft

Olá tenho que consumir um WebService utilizando apenas javascript

Encontrei um plugin (jqSOAPClient) do JQuery que faz isto...
Para teste eu procurei um WebService de exemplo na internet e o testei com o soapUI, funcionou sem problemas mas quando fui utilizar
O caso é que quando tento usar plugin para consumir o WS, ele gera o xml perfeitamente mas na hora de submeter a requisição ajax post gera um erro:

No firefox:
---------------------------
thrownError:
xhr.status: 0
xhr.responseText:
xhr.statusText: error

Internet Explorer
---------------------------
thrownError: No Transport
xhr.status: 0
xhr.responseText: undefined
xhr.statusText: error

Segue abaixo meu código, se alguém puder me ajudar: index.html
<html>
	<head>
	
		<script type="text/javascript" src="./js/jquery.1.6.min.js"></script>
		<script type="text/javascript" src="./js/jquery.SOAPClient.beta.js"></script>
		
		<script type="text/javascript">
			$(document).ready(function() {
				var soapBody = new SOAPObject("kir:GetMovieAtNumber");
				
				var soapSNumber = new SOAPObject("kir:input");
				soapSNumber.val("1");
				soapBody.appendChild(soapSNumber);
				
				var sr = new SOAPRequest("http://www.kirupafx.com/GetMovieAtNumber", soapBody); //Request is ready to be sent
				sr.addNamespace("kir", "http://www.kirupafx.com");
				
				SOAPClient.Proxy = "http://www.kirupafx.com/WebService/TopMovies.asmx"; //Specify web-service address or a proxy file

				//######### AQUI EU INVOCO O WS #########
				SOAPClient.SendRequest(sr, processResponse); //Send request to server and assign a callback
				//###################################
				
			});
			function processResponse(respObj) {
				alert('8');
				$('#divRetorno').html("<p>"+respObj+"</p>");
			}
		</script>
	</head>
	<body>
		<div id="divRetorno" style="border: 1px solid #ff0000; height: 200px;">
			<p>Executando...</p>
		</div>
	</body>
</html>
jquery.SOAPClient.beta.js
var SOAPClient = (function() {
	var httpHeaders = {};
	var _tId = null;
	var _self = {
		Proxy: "",
		SOAPServer: "",
		ContentType: "text/xml",
		CharSet: "utf-8",
		ResponseXML: null,
		ResponseText: "",
		Status: 0,
		ContentLength: 0,
		Timeout: 0,
		SetHTTPHeader: function(name, value){
			var re = /^[\w]{1,20}$/;
			if((typeof(name) === "string") && re.test(name)) {
				httpHeaders[name] = value;
			} 
		},
		Namespace: function(name, uri) {
			return {"name":name, "uri":uri};
		},
		SendRequest: function(soapReq, callback) {
			if(!!SOAPClient.Proxy) {
				SOAPClient.ResponseText = "";
				SOAPClient.ResponseXML = null;
				SOAPClient.Status = 0;
				
				var content = soapReq.toString();
				SOAPClient.ContentLength = content.length;
				
				function getResponse(xData) {
					alert('Success - xData: '+xData);
					if(!!_tId) {clearTimeout(_tId);}
						SOAPClient.Status = xhrReq.status;
						SOAPClient.ResponseText = xhrReq.responseText;
						SOAPClient.ResponseXML = xhrReq.responseXML;
					if(typeof(callback) === "function") {
						var jsOut = $.xmlToJSON(xData);
						callback(jsOut);
					}
				}//######### AQUI EU INVOCO O WS #########
				var xhrReq = $.ajax({
					 type: "POST",
					 url: SOAPClient.Proxy,
					 dataType: "xml",
					 processData: false,
					 data: content,
					 success: getResponse,
					 error: function (xhr, ajaxOptions, thrownError){
						alert('thrownError: '+thrownError+' \nxhr.status: '+xhr.status+' \nxhr.responseText: '+xhr.responseText+' \nxhr.statusText: '+xhr.statusText);
					 },
					 contentType: SOAPClient.ContentType + "; charset=\"" + SOAPClient.CharSet + "\"",
					 beforeSend: function(req) {
						req.setRequestHeader("Method", "POST");
						req.setRequestHeader("Content-Length", SOAPClient.ContentLength);
						if ((soapReq.Action!=undefined)&&(soapReq.Action.length>0))
							req.setRequestHeader("SOAPServer", SOAPClient.SOAPServer);
						if ((soapReq.Action!=undefined)&&(soapReq.Action.length>0))
							req.setRequestHeader("SOAPAction", soapReq.Action);
						
						if(!!httpHeaders) {
							var hh = null, ch = null;
							for(hh in httpHeaders) {
								if (!httpHeaders.hasOwnProperty || httpHeaders.hasOwnProperty(hh)) {
									ch = httpHeaders[hh];
									req.setRequestHeader(hh, ch.value);
								}
							}
						}
					 }//######### ATÉ AQUI #########
				});
			}
		},
		ToXML: function(soapObj) {
			var out = [];
			var isNSObj=false;
			try {
				if(!!soapObj&&typeof(soapObj)==="object"&&soapObj.typeOf==="SOAPObject") {
					//Namespaces
					if(!!soapObj.ns) {
						if(typeof(soapObj.ns)==="object") {
							isNSObj=true;
							out.push("<"+soapObj.ns.name+":"+soapObj.name);
							out.push(" xmlns:"+soapObj.ns.name+"=\""+soapObj.ns.uri+"\"");
						} else  {
							out.push("<"+soapObj.name);
							out.push(" xmlns=\""+soapObj.ns+"\"");
						}
					} else {
						out.push("<"+soapObj.name);
					}
					//Node Attributes
					if(soapObj.attributes.length > 0) {
						 var cAttr;
						 var aLen=soapObj.attributes.length-1;
						 do {
							 cAttr=soapObj.attributes[aLen];
							 if(isNSObj) {
								out.push(" "+soapObj.ns.name+":"+cAttr.name+"=\""+cAttr.value+"\"");
							 } else {
								out.push(" "+cAttr.name+"=\""+cAttr.value+"\"");
							 }
						 } while(aLen--);
					}
					out.push(">");
					//Node children
					if(soapObj.hasChildren()) {
						var cPos, cObj;
						for(cPos in soapObj.children){
							cObj = soapObj.children[cPos];
							if(typeof(cObj)==="object"){out.push(SOAPClient.ToXML(cObj));}
						}
					}
					//Node Value
					if(!!soapObj.value){out.push(soapObj.value);}
					//Close Tag
					if(isNSObj){out.push("</"+soapObj.ns.name+":"+soapObj.name+">");}
					else {out.push("</"+soapObj.name+">");}
					return out.join("");
				}
			} catch(e){alert("Unable to process SOAPObject! Object must be an instance of SOAPObject");}
		}
	};
	return _self;
})();
//Soap request - this is what being sent using SOAPClient.SendRequest
var SOAPRequest=function(action, soapObj) {
	this.Action=action;	
	var nss=[];
	var headers=[];
	var bodies=(!!soapObj)?[soapObj]:[];
	this.addNamespace=function(ns, uri){nss.push(new SOAPClient.Namespace(ns, uri));};
	this.addHeader=function(soapObj){headers.push(soapObj);};
	this.addBody=function(soapObj){bodies.push(soapObj);};
	this.toString=function() {
		var soapEnv = new SOAPObject("soapenv:Envelope");
			soapEnv.attr("xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
		//Add Namespace(s)
		if(nss.length>0){
			var tNs, tNo;
			for(tNs in nss){if(!nss.hasOwnProperty || nss.hasOwnProperty(tNs)){tNo=nss[tNs];if(typeof(tNo)==="object"){soapEnv.attr("xmlns:"+tNo.name, tNo.uri);}}}
		}
		//Add Header(s)
		if(headers.length>0) {
			var soapHeader = soapEnv.appendChild(new SOAPObject("soapenv:Header"));
			var tHdr;
			for(tHdr in headers){if(!headers.hasOwnProperty || headers.hasOwnProperty(tHdr)){soapHeader.appendChild(headers[tHdr]);}}
		}
		//Add Body(s)
		if(bodies.length>0) {
			var soapBody = soapEnv.appendChild(new SOAPObject("soapenv:Body"));
			var tBdy;
			for(tBdy in bodies){if(!bodies.hasOwnProperty || bodies.hasOwnProperty(tBdy)){soapBody.appendChild(bodies[tBdy]);}}
		}
		return soapEnv.toString();		
	};
};

//Soap Object - Used to build body envelope and other structures
var SOAPObject = function(name) {
	this.typeOf="SOAPObject";
	this.ns=null;
	this.name=name;
	this.attributes=[];
	this.children=[];
	this.value=null;
	this.attr=function(name, value){this.attributes.push({"name":name, "value":value});return this;};
	this.appendChild=function(obj){this.children.push(obj);return obj;};
	this.hasChildren=function(){return (this.children.length > 0)?true:false;};
	this.val=function(v){if(!v){return this.value;}else{this.value=v;return this;}};
	this.toString=function(){return SOAPClient.ToXML(this);};
};
Criado 9 de maio de 2011
Respostas 0
Participantes 1