Java cors无法从jquery调用web服务
我已经用axis2创建了一个web服务,并用eclipse在ApacheTomcat7中进行了部署 这是我在web服务中转换的类。这只是一个回声:Java cors无法从jquery调用web服务,java,jquery,web-services,axis2,cors,Java,Jquery,Web Services,Axis2,Cors,我已经用axis2创建了一个web服务,并用eclipse在ApacheTomcat7中进行了部署 这是我在web服务中转换的类。这只是一个回声: package test.org; public class TestWS { public String echo(String s){ return ("You typed: " + s); } } 如果我从Eclipse尝试这个web服务,在web服务浏览器中添加wsdl,它会非常好地工作,但是当我尝试从jqu
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无法加载http://localhost:8080/SimpleWS/services/TestWS
。访问控制允许原点不允许原点为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>
Web服务示例
函数callWS(){
var soapmessage=“”;
soapmessage+=“”;
soapmessage+=“”;
soapmessage+=“你好”;
soapmessage+=“”;
soapmessage+=“”;
soapmessage+=“”;
警报(soapmessage);
$.ajax({
键入:“POST”,
网址:'http://localhost:8080/SimpleWS/services/TestWS',
数据:soapmessage,
contentType:“应用程序/xml;字符集=utf-8”,
数据类型:“xml”,
成功:功能(数据){
警报(数据);
},
错误:函数(数据){
警报(“错误”+data.d);
}
});
警报(“提交的表格”);
}
我建议用一种更简单的方法来做同样的事情
功能可能是:
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>
另见:
null
不被Access Control Allow Origin
最可能的情况是,您正在使用本地文件通过XMLHttpRequest
发出请求。e、 g.来自:
file:///C:/Users/Paul/Desktop/index.html
域、协议和端口与WebService的URL不同。维基百科是这样说的:
术语“源”是使用域名、应用层协议和(在大多数浏览器中)运行脚本的HTML文档的端口号定义的。当且仅当所有这些值完全相同时,两个资源被视为具有相同的来源
尝试使用相同来源的文件,例如:
谢谢你的回答。我完全按照你说的做了,但我仍然有同样的问题。Chrome控制台显示:XMLHttpRequest无法加载。访问控制允许原点不允许原点为null。我得到了一个错误警报。我想一定是域名的问题……可能是什么?
file:///C:/Users/Paul/Desktop/index.html
http://localhost:8080/SimpleWS/index.jsp