我试图在我的球衣网络服务器中启用CORS。我看了this page。所以我创建了我的过滤器
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
@Provider
public class CORSFilter implements ContainerResponseFilter {
@Override
public void filter(final ContainerRequestContext requestContext,
final ContainerResponseContext responseContext) throws IOException {
final MultivaluedMap<String, Object> headers = responseContext
.getHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Credentials", "true");
headers.add("Access-Control-Allow-Headers",
"X-Requested-With, Content-Type, X-Codingpedia, ");
}
}
这是我的jQuery测试
$.ajax({
type: 'GET',
url: myURL,
crossDomain: true,
dataType: 'jsonp',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data.statusText);
console.log(data.name);
},
headers: {
"Authorization": "Basic " + btoa('login:pass'),
"Accept": "application/json"
}
});
当我运行这个js时,它一直问我登录名和密码,即使我输入的权利总是返回401未经授权。
答案 0 :(得分:0)