2011-08-05 22 views
10

TodoSolicitud SOAP múltiple usando Javascript

Estoy usando mi API SOAP usando script java.

este ejemplo explica cómo enviar la solicitud única de jabón usando 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... 

Ahora quiero enviar solicitud jabón múltiples a la vez (media de más de una solicitud envoltura).

+3

Nada debería impedir que envíe otra solicitud ('nueva XMLHttpRequest()') inmediatamente después de esta. Es un envío asincrónico. – namuol

+0

este es el problema http://stackoverflow.com/questions/6922058/what-is-wrong-this-js-got-this-error-cannot-use-object-of-type-stdclass-as-ar –

+0

hai i estoy usando su código pero xmlhttp.responseText resulta nulo. – user969275

Respuesta

6

Siempre que el tercer parámetro en XMLHttpRequest.open se establezca en verdadero, la llamada será asincrónica. Entonces debería poder enviar uno nuevo sin mucho esfuerzo. Necesita un nuevo objeto XMLHttpRequest para que funcione.

Si desea utilizar la misma devolución de llamada, puede definirla como una función y usar this para trabajar con el objeto de solicitud.

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

me enfrento a este problema http://stackoverflow.com/questions/6922058/what-is-wrong-this-js-got-this-error-cannot-use-object-of-type-stdclass-as-ar –

+0

Ese problema se debe a un error en el servicio SOAP y no está relacionado con esta pregunta. – wazz3r

Cuestiones relacionadas