2015-04-01 223 views
2

我正在用JS创建一个SOAP客户端,这是我第一次使用web服务,并且我的代码中必须有几个错误。创建一个JavaScript SOAP客户端

poit是,用我的代码,我无法访问到WebService,但我不知道如何访问里面的方法。所以web服务给我以下的回应:

<h1>Version</h1> 
<p>Hi there, this is an AXIS service!</p> 
<i>Perhaps there will be a form for invoking the service here...</i> 

而不是正在调用的方法正确的XML。

这是我的SOAP客户端的代码:

<html> 

<script type="text/javascript"> 

    function run(){ 

     var objXMLHttpRequest = new XMLHttpRequest(); 
     objXMLHttpRequest.open("GET", "http://localhost:8080/CrunchifyWS/services/Version?wdsl/", true); 
     objXMLHttpRequest.onreadystatechange = function() { 
     //alert(objXMLHttpRequest.readyState+" "+ objXMLHttpRequest.status); 
     if (objXMLHttpRequest.readyState == 4 && objXMLHttpRequest.status == 200) { 
      result = objXMLHttpRequest.responseXML; 
      alert(objXMLHttpRequest.responseText); 
      alert(objXMLHttpRequest.responseXML); 
      } 
     } 
     objXMLHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 

     var packet = '<?xml version="1.0" encoding="utf-8" ?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><getVersion xmlns="http://service.web.com.crunchify/"></getVersion></soap:Body></soap:Envelope>'; 

     objXMLHttpRequest.send(packet); 
     // Add mentioned below code only if your application calls webservice synchronously. Otherwise use "onreadystatechange" process response. 
     response = objXMLHttpRequest.responseXML; 
     alert(objXMLHttpRequest.responseText+ " "+ response); 
    } 
</script> 

<head> 
</head> 
<body> 
    <button onclick="run()">Dale!</button> 
</body> 
</html> 

我认为TE的错误应该是在部分,但是,正如我所说,这是我第一次,我不知道我正在做。

解决

+0

我对角读取,但尝试“POST”而不是“GET”?大多数肥皂终点是仅邮政。 – nablex 2015-04-01 10:05:11

+0

当尝试POST而不是GET我有 <?xml version =“1.0”encoding =“UTF-8”?> NS1:Client.NoSOAPAction 没有SOAPAction头 WebServices-PC LokiNkc 2015-04-01 10:12:19

+0

是的,在SOAP 1.1版本中,您需要添加一个soapaction。这个soapaction可以从WSDL中获取,并且必须作为HTTP头传入。 – nablex 2015-04-01 10:15:47

回答

0

的第一个问题可能是在这条线:

objXMLHttpRequest.open("GET", "http://localhost:8080/CrunchifyWS/services/Version?wdsl/", true); 

这个网址 http://localhost:8080/CrunchifyWS/services/Version?wsdl 给你,你可以调用服务的列表。

所以你可能有这样的事情:

objXMLHttpRequest.open("GET", "http://localhost:8080/CrunchifyWS/services/myServiceMethod", true); 

这是第一个问题,我想。 第二次,你必须发送你的参数,但不是整个Soap信封。

var packet = '<?xml version="1.0" encoding="utf-8" ?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><getVersion xmlns="http://service.web.com.crunchify/"></getVersion></soap:Body></soap:Envelope>'; 

objXMLHttpRequest.send(packet); 

喜欢:

var parameters = "param1=15&param2=30" 
objXMLHttpRequest.send(parameters); 

,并且该方法的发送应该出的 objXMLHttpRequest.onreadystatechange =函数(){}

此函数是一个回调审查响应。

不要犹豫,看看这个链接: http://www.w3schools.com/xml/xml_dtd.asp

希望这有助于

+0

事情是这个方法没有参数。 这是WS的wdsl:http://pastebin.com/3QejHfqz 我真的不知道我必须这样做:( – LokiNkc 2015-04-01 09:57:15

0

您可以查看评论的答案的发展,但在短期:

1)SOAP方法调用需要使用HTTP方法“POST”,所以GET操作是第一个问题。

2)在SOAP 1.1中,服务器需要SOAPAction头以知道您尝试调用哪种方法。当使用“文档文字包装”方法时,这并不是严格必要的,许多SOAP 1.1服务器实际上接受空soap操作,因为它们可以根据请求的根标签找到方法,因此提供了与SOAP 1.2的一些前向兼容性。

但是严格的SOAP 1。1个服务器将需要SOAPAction头沿着正确发送:

objXMLHttpRequest.setRequestHeader("SOAPAction", "mySoapAction"); 

哪里可以找到您试图在Web服务的原始WSDL调用的方法相应的SOAPAction。