javascript - How to Call a Web Service in a Cross-Browser way -



javascript - How to Call a Web Service in a Cross-Browser way -

i want phone call web service mozilla, net explorer , chrome.

bellow laboratoryservice.js file calls web service:

function stringbuffer() { this.__strings__ = new array; } stringbuffer.prototype.append = function (str) { this.__strings__.push(str); }; stringbuffer.prototype.tostring = function () { homecoming this.__strings__.join(""); }; function laboratoryservice() { this.url = "http://25.48.190.93:8082/labratory?wsdl"; } laboratoryservice.prototype.buildrequest = function () { var obuffer = new stringbuffer(); obuffer.append("<soap:envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "); obuffer.append("xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" "); obuffer.append("xmlns:xsd=\"http://www.w3.org/2001/xmlschema\">"); obuffer.append("<soap:body>"); obuffer.append("<getlabratory xmlns=\"http://nano.ito.ir/\" />"); obuffer.append("</soap:body>"); obuffer.append("</soap:envelope>"); homecoming obuffer.tostring(); }; laboratoryservice.prototype.send = function () { var orequest = new xmlhttprequest; orequest.open("post", this.url, false); orequest.setrequestheader("content-type", "text/xml"); orequest.setrequestheader("soapaction", this.action); orequest.send(this.buildrequest()); if (orequest.status == 200) { homecoming this.handleresponse(orequest.responsetext); } else { throw new error("request did not complete, code " + orequest.status); } }; laboratoryservice.prototype.handleresponse = function (sresponse) { var start = sresponse.indexof('div') - 4; var end = sresponse.lastindexof('div') + 7; homecoming sresponse.substring(start, end); };

bellow html code uses laboratoryservice.js show data:

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>get labratories</title> <script language="javascript" src="laboratoryservice.js"></script> <script language="javascript" src="jquery-1.8.0.min.js"></script> <script language="javascript" type="text/javascript"> $(document).ready(function () { $("#btngetlaboratories").click(function () { var oservice = new laboratoryservice(); var fresult = oservice.send(); var newdata = $('<div/>').html(fresult).text(); $("#divresult").html(newdata); }); }); </script> </head> <body> <input id="btngetlaboratories" type="button" value="get laboratories" /> <div id="divresult"> </div> </body> </html>

this approach works fine in net explorer. problem approach not work in firefox , chrome. think orequest.send(this.buildrequest()); not work in firefox , chrome.

edited web service phone call using jquery

i changed laboratoryservice.prototype.send utilize jquery phone call web service bellow:

laboratoryservice.prototype.send = function () { $.ajax({ type: "post", url: this.url, contenttype: "text/xml", headers: { "soapaction": this.action }, success: function (msg) { homecoming this.handleresponse(msg); }, error: function (e) { alert('error'); } }); };

but alerts error. how phone call web service using jquery?

again edited code

i changed jquery ajax phone call bellow. works fine in net explorer returns error in chrome , firefox.

laboratoryservice.prototype.send = function () { $.ajax({ type: "post", url: this.url, contenttype: "text/xml; charset=\"utf-8\"", datatype: "xml", data: this.buildrequest(), processdata: false, success: function processsuccess(data, status, req) { if (status == "success") { var sresponse = req.responsetext; var start = sresponse.indexof('div') - 4; var end = sresponse.lastindexof('div') + 7; var newdata = $('<div/>').html(sresponse.substring(start, end)).text(); $("#divresult").html(newdata); } }, error: function () { alert('error'); } }); };

just alter :

laboratoryservice.prototype.send = function () { var orequest = new xmlhttprequest; orequest.open("post", this.url, true); orequest.setrequestheader('user-agent','xmlhttp/1.0'); orequest.setrequestheader("content-type", "application/x-www-form-urlencoded"); orequest.setrequestheader("soapaction", this.action); orequest.send(this.buildrequest()); if (orequest.status == 200) { homecoming this.handleresponse(orequest.responsetext); } else { throw new error("request did not complete, code " + orequest.status); } };

please, refer link.

javascript web-services xmlhttprequest

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -