简体   繁体   English

使用Javascript的多个SOAP请求

[英]Multiple SOAP request using Javascript

All 所有

I am using my SOAP API using java script. 我正在使用Java脚本使用SOAP API。

this example explain how to send single soap request using js 本示例说明如何使用js发送单个soap请求

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). 现在,我想一次发送多个肥皂请求(平均请求多个信封)。

As long as the third parameter in XMLHttpRequest.open is set to true the call will be asynchronous. 只要将XMLHttpRequest.open的第三个参数设置为true,调用将是异步的。 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. 您确实需要一个新的XMLHttpRequest对象才能使其工作。

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. 如果要使用相同的回调,则可以将其定义为一个函数,然后将this用于请求对象。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM