【问题标题】:cors trouble calling web service from jquerycors 无法从 jquery 调用 Web 服务
【发布时间】:2013-04-08 01:01:41
【问题描述】:

我使用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>

【问题讨论】:

    标签: java jquery web-services axis2 cors


    【解决方案1】:

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

    函数可能是:

    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>
    

    另见:

    关于Origin null 不允许Access-Control-Allow-Origin

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

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

    域、协议和端口与WebService 的URL 不同。维基百科在Same origin policy 中提到了这一点:

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

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

    http://localhost:8080/SimpleWS/index.jsp
    

    【讨论】:

    • 谢谢你的回答。我完全按照你说的做了,但我仍然有同样的问题。 Chrome 控制台说:XMLHttpRequest 无法加载localhost:8080/SimpleWS/services/TestWS/echo。 Access-Control-Allow-Origin 不允许 Origin null。我收到错误警报。我认为它必须与域有关......它可能是什么?
    猜你喜欢
    • 2015-05-05
    • 1970-01-01
    • 2012-06-18
    • 2015-08-22
    • 2010-12-29
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多