所有
我使用我的SOAP API使用java脚本.
这个例子说明如何使用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...
现在我想一次发送多个soap请求(平均请求多个信封).
解决方法
只要XMLHttpRequest.open中的第三个参数设置为true,调用将是异步的.所以你应该能够发送一个新的没有太多的努力.您需要一个新的XMLHttpRequest对象才能正常工作.
如果要使用相同的回调函数,您可以将其定义为函数,并使用它来处理请求对象.
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",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",true); xmlhttp2.onreadystatechange=soapCallback; xmlhttp2.setRequestHeader("SOAPAction","http://www.webserviceX.NET/GetQuote"); xmlhttp2.setRequestHeader("Content-Type","text/xml"); xmlhttp2.send(xml);