0

我用axis2创建了一个web服务,我用eclipse部署在apache tomcat 7中。

这是我在 Web 服务中转换的类。这只是一个回声:

package test.org;
public class TestWS {
    public String echo(String s){
        return ("You typed: " + s);
    }
}

如果我从 Eclipse 中尝试这个 Web 服务,在 Web 服务资源管理器中添加 wsdl 它可以完美地工作,但是当我尝试从 jquery 使用这个 ws 时问题就来了。我可以毫无问题地访问 wsdl,它位于: http://localhost:8080/SimpleWS/services/TestWS?wsdl

但是,当我尝试这段代码时,会发生以下错误: XMLHttpRequest cannot load http://localhost:8080/SimpleWS/services/TestWS。Access-Control-Allow-Origin 不允许 Origin null。

我一直在阅读有关 CORS 以及在服务器端包含一些类似这样的代码:Access-Control-Allow-Origin: *,但是由于我已经直接使用 eclipse 部署了我的 ws,所以我不知道该怎么做,并且我不确定这是否是我的问题。

先感谢您!!

这是我的代码:

<h3>Web service example</h3>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

function callWS() {
    var soapmessage = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' " +
                                    "xmlns:q0='http://org.test' xmlns:xsd='http://www.w3.org/2001/XMLSchema'" +
                                    "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
                      soapmessage += "<soapenv:Body>";
                      soapmessage += "<q0:echo>";
                      soapmessage += "<q0:s>HELLO</q0:s>";
                      soapmessage += "</q0:echo>";
                      soapmessage += "</soapenv:Body>";
                      soapmessage += "</soapenv:Envelope>";
                      alert(soapmessage);
                      $.ajax({
                          type: 'POST',
                          url: 'http://localhost:8080/SimpleWS/services/TestWS',
                          data: soapmessage,
                          contentType: "application/xml; charset=utf-8",
                          dataType: "xml",
                          success: function (data) {
                             alert(data);
                          },
                          error: function (data) {
                              alert("error" + data.d);
                          }
                     });
                     alert("Form Submitted");
}

</script>

<form method="post" action="">
    <input type="button" onclick="callWS()" value="execute" />
</form>
</body>
</html>
4

1 回答 1

0

我会建议一种更简单的方法来做同样的事情。

该功能可能是:

function callWS() {
    $.ajax({
        url: "http://localhost:8080/SimpleWS/services/TestWS/echo", 
        type: "POST",
        data: { s : "HELLO" }, 
        success: function(data, textStatus, jqXHR) {
            alert($(data).find("return").text());
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert(jqXHR.status + " " + jqXHR.statusText);
        }
    });
}

在 URL 中添加了要调用的操作的名称。数据是一个映射,每个参数都有键值对。

操作的名称和键的名称必须与 WSDL 文档匹配,包括响应中每个元素的名称。您可以通过以下方式查看架构文件:

http://localhost:8080/SimpleWS/services/TestWS?xsd

这给出了类似的东西:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ... >
  <xs:element name="echo">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" name="s" nillable="true" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="echoResponse">
    <xs:complexType>
      <xs:sequence>
       <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

该文件列出了两种主要类型(对于本示例),带有元素的请求和带有s元素的响应return

WSDL 显示操作的名称。

<wsdl:binding name="TestWSHttpBinding" type="ns:TestWSPortType">
  <http:binding verb="POST"/>
  <wsdl:operation name="echo">
    <http:operation location="echo"/>
    <wsdl:input>
      <mime:content type="application/xml" part="parameters"/>
    </wsdl:input>
    <wsdl:output>
      <mime:content type="application/xml" part="parameters"/>
    </wsdl:output>
  </wsdl:operation>
</wsdl:binding>

也可以看看:

关于原产地null是不允许的Access-Control-Allow-Origin

很可能您正在XMLHttpRequest使用本地文件发出请求。例如来自:

file:///C:/Users/Paul/Desktop/index.html

域、协议和端口与 WebService 的 URL 不同。维基百科在同源政策中谈到了这一点:

术语“来源”是使用运行脚本的 HTML 文档的域名、应用层协议和(在大多数浏览器中)端口号来定义的。当且仅当所有这些值完全相同时,才认为两个资源具有相同的来源。

尝试使用同一来源的文件,例如:

http://localhost:8080/SimpleWS/index.jsp
于 2013-04-17T06:44:41.183 回答