2011-08-05 32 views
10

WszystkieWiele żądań SOAP przy użyciu kodu JavaScript

Używam mojego interfejsu SOAP API przy użyciu skryptu Java.

ten przykład wyjaśnić, jak wysłać prośbę jednego mydła używając 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... 

Teraz chcę wysłać wielokrotne żądania SOAP w czasie (średnio więcej niż jeden wniosek kopertę).

+3

Nic nie powinno powstrzymywać Cię przed wysłaniem kolejnego żądania ('new XMLHttpRequest()') zaraz po tym. Jest to wysyłanie asynchroniczne. – namuol

+0

to jest problem http://stackoverflow.com/questions/6922058/what-is-wrong-this-js-got-this-error-cannot-use-object-of-type-stdclass-as-ar –

+0

hai i używam twojego kodu, ale xmlhttp.responseText ma wartość null. – user969275

Odpowiedz

6

Dopóki trzeci parametr w XMLHttpRequest.open jest ustawiony na wartość true, wywołanie będzie asynchroniczne. Więc powinieneś być w stanie wysłać nowy bez większego wysiłku. Potrzebny jest nowy obiekt XMLHttpRequest, aby działał.

Jeśli chcesz użyć tego samego wywołania zwrotnego, możesz po prostu zdefiniować go jako funkcję i użyć this do pracy z obiektem żądania.

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

Mam do czynienia z tym problemem http://stackoverflow.com/questions/6922058/what-is-wrong-this-js-got-this-error- cannon-use-object-of-type-stdclass-as-ar –

+0

Ten problem jest spowodowany błędem w usłudze SOAP i nie jest związany z tym pytaniem. – wazz3r

Powiązane problemy