2011-08-05 9 views
10

すべてJavascriptを使用した複数のSOAPリクエスト

私はJavaスクリプトを使用してSOAP APIを使用しています。

この例

は(要求、複数の封筒を意味する)

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要求を送信したいのjs使用して、単一のSOAPリクエストを送信する方法について説明します。

+3

この直後に別のリクエスト(新しいXMLHttpRequest()を送信しないようにする必要はありません)。それは非同期送信です。 – namuol

+0

これは問題です。http://stackoverflow.com/questions/6922058/what-is-wrong-this-js-got-this-error-cannot-use-object-of-type-stdclass-as-ar –

+0

hai iあなたのコードを使用したが、xmlhttp.responseTextはヌル結果います。 – user969275

答えて

6

限りXMLHttpRequest.openにおける第三のパラメータは、コールが非同期であろうtrueに設定されます。だからあなたは、単に多くの努力なしに新しいものを送ることができるはずです。それを動作させるためには、新しいXMLHttpRequestオブジェクトが必要です。

あなたが同じコールバックを使用したい場合は、あなただけの関数として定義し、リクエストオブジェクトで動作するように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); 
+0

私はこの問題に直面していますhttp://stackoverflow.com/questions/6922058/what-is-wrong-this-js-got-this-error-cannot-use-object-of-type-stdclass-as-ar –

+0

その問題は、SOAPサービスのエラーが原因であり、この質問とは関係ありません。 – wazz3r

関連する問題