从服务器获取SOAP响应

时间:2012-03-03 07:44:26

标签: javascript web-services soap soapui soap-client

我试着根据它调用SOAP调用 Simplest SOAP example

我可以通过此代码发送请求,但没有来自服务器的响应。我在下面给出了示例代码:

在这里输入代码

<html>



   <head>
     <title>SOAP call sample</title>
     <script language="Javascript">
     <!--     

     function xmlhttpPost() {
      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("ready state callback:"+xmlhttp.readyState);

     alert("response text or XML"xmlhttp.responseText);

     var json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);

  var result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;

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

xmlhttp.setRequestHeader("POST","/stockquote.asmx HTTP/1.1");

xmlhttp.setRequestHeader("Host","www.webservicex.net");

xmlhttp.setRequestHeader("Content-Length",1000);

alert("setrequest header completed");

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

alert("request sent"+xmlhttp);

     }
//-->
  </script>

</head>

<form name="main">

  <table>

     <tr>

       <td> <input value="Submit to eBay => " type="button" 
onclick='JavaScript:xmlhttpPost()'></td>
       <td><textarea name="eBayXMLResponse" wrap="soft" rows="40" cols="50" style="overflow:scroll" ID="Textarea1"></textarea></td>
     </tr>
  </table>
 </form>

</html>

我通过eclipse尝试使用动态Web项目并通过apache-tomcat-7.0.25应用服务器运行此示例。这足以运行此示例吗? 请帮我在浏览器控制台中显示响应。 我在这个问题上挣扎了一个星期......如果有人对此有所了解,请告诉我。

1 个答案:

答案 0 :(得分:0)

尝试使用jquery发出soap请求。这对我有用:

var soapAction = this.Namespace + this.Contract + '/' + pMethod;
var soapResponse = pMethod + 'Response', soapResult = pMethod + 'Result';
$.ajax({
    type: "POST",
    url: this.URI,
    data: envelope,
    contentType: "text/xml",
    dataType: "xml",
    beforeSend: function (xhr) {
        xhr.setRequestHeader("SOAPAction", soapAction);
    },
    success: function (pData) {
        var answer;
        $(pData).find(soapResponse).each(function () {
            answers=this.parseResult(($(this).find(soapResult))[0]);
        });
        onSuccess(answers);
    },
    error: onError
});