Когда код angular пытается выполнить вызов localhost:8088
Я получите ошибку
Доступ к XMLHttpRequest по адресу «http://localhost:8088/api/parse/» из источника «http://localhost:8080» был заблокирован политикой CORS: ответ на предполетный запрос не проходит проверку контроля доступа: у него нет статуса HTTP ok.
Мой угловой код выглядит например (вызов с локального хоста: 8080)
Код: Выделить всё
$http({
method : 'POST',
url : 'http://localhost:8088/api/parse/',
crossDomain: true,
cache:false,
data : $.param( {
vid: vid,
file: file
}),
headers : { 'Content-Type': 'application/json; charset=UTF-8' }
})
.success(function(data) {
return data;
}).
error(function(data, status, headers, config) {
return data;
});
Когда я использую отладчик, я знаю, что фильтр вызывается.
Код: Выделить всё
@Provider
public class CORSFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
responseContext.getHeaders().add("Access-Control-Max-Age", "1209600");
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "*");
responseContext.getHeaders().add("Access-Control-Allow-Methods", "*");
}
}
Код: Выделить всё
@ApplicationPath("/api")
public class RestApplication extends Application {
private Set singletons = new HashSet();
public RestApplication() {
singletons.add(new CORSFilter());
}
@Override
public Set getSingletons() {
return singletons;
}
}
Код: Выделить всё
@POST
@Consumes("application/json")
@Produces("application/json")
@Path("/parse")
public Response parsePost(String jsonPayload, @Context HttpHeaders headers) {
try {
String json = "{\"success\":\"ok\"+}";
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}catch (NotAuthorizedException ex){
return Response.status(Response.Status.UNAUTHORIZED).entity(ex.getMessage()).build();
}catch (Exception ex){
ex.printStackTrace();
return Response.serverError().entity(ex.getMessage()).build();
}finally {
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... cors-error