Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple SOAP request using Javascript

All

I am using my SOAP API using java script.

this example explain how to send single soap request using js

var symbol = "MSFT"; 
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
xmlhttp.onreadystatechange=function() {
 if (xmlhttp.readyState == 4) {
  alert(xmlhttp.responseText);
  // http://www.terracoder.com convert XML to JSON 
  var json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);
  var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;
  // Result text is escaped XML string, convert string to XML object then convert to JSON object
  json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result));
  alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text); 
 }


}
xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
 '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + 
   '<soap:Body> ' +
     '<GetQuote xmlns="http://www.webserviceX.NET/"> ' +
       '<symbol>' + symbol + '</symbol> ' +
     '</GetQuote> ' +
   '</soap:Body> ' +
 '</soap:Envelope>';
xmlhttp.send(xml);
// ...Include Google and Terracoder JS code here...

Now i want to send multiple soap request at a time (mean request more than one envelop).

like image 771
Ajay Patel Avatar asked Aug 05 '11 11:08

Ajay Patel


1 Answers

As long as the third parameter in XMLHttpRequest.open is set to true the call will be asynchronous. So your should just be able to send a new one without much effort. You do need a new XMLHttpRequest object for it to work.

If you want to use the same callback you can just define it as a function and use this to work with the request object.

function soapCallback() {
    if (this.readyState == 4) {
        alert(this.responseText);
        // http://www.terracoder.com convert XML to JSON 
        var json = XMLObjectifier.xmlToJSON(this.responseXML);
        var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;
        // Result text is escaped XML string, convert string to XML object then convert to JSON object
        json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result));
        alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text); 
    }
}

var symbol = "MSFT"; 
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
 '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + 
   '<soap:Body> ' +
     '<GetQuote xmlns="http://www.webserviceX.NET/"> ' +
       '<symbol>' + symbol + '</symbol> ' +
     '</GetQuote> ' +
   '</soap:Body> ' +
 '</soap:Envelope>';

var xmlhttp1 = new XMLHttpRequest();
xmlhttp1.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
xmlhttp1.onreadystatechange=soapCallback;
xmlhttp1.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp1.setRequestHeader("Content-Type", "text/xml");
xmlhttp1.send(xml);

var xmlhttp2 = new XMLHttpRequest();
xmlhttp2.open("POST", "http://www.webservicex.net/stockquote.asmx?op=GetQuote",true);
xmlhttp2.onreadystatechange=soapCallback;
xmlhttp2.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp2.setRequestHeader("Content-Type", "text/xml");
xmlhttp2.send(xml);
like image 63
wazz3r Avatar answered Oct 02 '22 19:10

wazz3r