我试图通过AJAX调用Java REST API。它仅适用于Firefox和Chrome浏览器,但它不适用于Microsoft Edge和IE。它显示如下错误:
" SCRIPT7002:XMLHttpRequest:网络错误0x2efd,无法完成 由于错误00002efd的操作。"和" SCRIPT7002: XMLHttpRequest:网络错误0x80070005,访问被拒绝。"。
我的AJAX电话是:
第一个:
$(function getTest() {
var url = 'http://localhost:8080/MyService/user/getListOfGender?_=1487141734780';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function(e) {
var arraybuffer = JSON.parse(xhr.responseText);
console.log(arraybuffer.Result);
};
xhr.send();
});
第二种方法:
$(function getGenderId() {
var sessionId = getSessionId();
$.ajax({
url: "http://localhost:8080/MyService/user/getListOfGender?_=1487141734780",
type: 'GET',
crossDomain: true,
dataType: 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader('sessionId', sessionId);
xhr.setRequestHeader('Accept',
'text/html, application/xhtml+xml, image/jxr, */*');
},
success: function(data) {
var sData = data.Result;
var genderData = JSON.parse(sData);
for (var i = 0; i < genderData.length; i++) {
var genderId = genderData[i].genderId;
var genderName = genderData[i].gender;
$("#gender").append(
'<option id="' + genderId + '">' + genderName +
'</option>');
}
},
error: function(e) {
// alert("Error" + e);
if (e.status == "401") {
$("#sessionModal").modal();
}
},
cache: false,
contentType: false,
processData: false
});
});
和我的其他api服务
@Path("/getListOfGender")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getListOfGender() {
String response = dataFetchService.getListOfGender();
return response;
}
和我的服务器角色配置
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowGenericHttpRequests</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowSubdomains</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, HEAD, POST, OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>origin, accept,Content-Type, X-Requested-With, sessionId</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>X-Test-1, X-Test-2</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.maxAge</param-name>
<param-value>-1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我试图从IE和Edge调用服务器,但它不起作用。让我知道是否需要从客户端或服务器端进行更改,如果需要的话。提前谢谢。