2012-03-03 136 views
1

SOAP响应我试图根据 Simplest SOAP example从服务器获取

我可以通过这个代码发送请求,但没有来自服务器无响应,使SOAP调用。下面的示例代码我给出:

输入代码在这里

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

我通过月食尝试这个样品与动态Web项目,并通过Apache,Tomcat的7.0.25运行应用程序server.Is这足够运行该样品? 请帮我在浏览器控制台中显示响应。 我在这个问题上挣扎了一周.....请让我知道是否有人对此有所了解。

+0

也许你需要在客户端和服务器之间的数据包捕获,看看发生了什么。 – ciphor 2012-03-04 09:46:39

+0

嗨ciphor,请告诉我清楚得到SOAP响应需要什么? – Mahes 2012-03-04 09:57:00

+0

有多种可能的原因,1.请求未发出; 2.服务器不工作; 3.回复不发送;数据包捕获将帮助您澄清您遇到的情况。 – ciphor 2012-03-04 11:58:49

回答

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