我无法从localhost调用我的休息服务,所以我创建了一个过滤器类,并在下面提到了代码。
过滤类代码:
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
response.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
response.addHeader("Access-Control-Max-Age", "1728000");
现在可以调用它但是当我将代码移动到我们有loadbalancer的QA时,再次无法调用并获得与下面提到的相同的错误
错误: XMLHttpRequest无法加载https://scv-qa.com/services/sur/data。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' http://www.w3schools.com'因此不允许访问。
Ajax电话:
$.ajax({
type: "POST",
url: "https://scv-qa.com/services/sur/data",
data: JSON.stringify({surId:1}),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(1);
},
error: function (jqXHR) {
alert(2);
}
});