0

I am having trouble consuming a rest service that did. Already implemented a CORS filter in my Java application, but when I use the $.ajax() it returns the problem of CORS. Using $.getJSON can access the method, however I can not add headers on it.

I need to know how to add the header in $.getJSON or how to make it recognize the CORS I added in my Java service.

I'm using Java 8, Wildfly 8.1-Final, RestEasy, etc.

My ajax like this:

var url = "http://pacoteiro-trysoft.rhcloud.com/api/v1/pacote/RC824435750CN";

$.ajax({
    async : true,
    type:'GET',
    contentType: 'application/json',
    url: url,
    cache:false,
    dataType: 'json',
    headers: {
        "token":"123"
    },

    success: function(data) { 
        alert("Cadastrado com sucesso!", data);
    },



 error: function(data) {
        alert("Erro!", data);
    }
});

and my filter this way:

@Provider
public class ConfigureCORSFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext,ContainerResponseContext responseContext) throws IOException {
          responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
          responseContext.getHeaders().add("Access-Control-Allow-Headers", "application/json, origin, content-type, accept, authorization");
          responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
          responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
          responseContext.getHeaders().add("Access-Control-Max-Age", "1209600");
    }

}
8
  • "but when I use the $.ajax() it returns the problem of CORS" what problem with CORS, any error message?
    – Braiam
    Aug 22, 2014 at 1:52
  • in which domain you working on? localhost or directory system or the server?
    – V31
    Aug 22, 2014 at 1:55
  • I don't get it - I never had to do anything special on the client side, its the -browser- that adds the extra CORS headers to the request. I second the question asked: what error are you getting?
    – Gimby
    Aug 22, 2014 at 7:19
  • @Braiam when i use it shows me a message to put the script on the same server that the rest service. but when i use $.getJSON it detects a CORS filter present in my code Java, but i dont know put headers into a $.getJSON.
    – Pedro HOS
    Aug 22, 2014 at 11:27
  • @V31 I working in a server on Openshift. Wildfly 8.1-Final
    – Pedro HOS
    Aug 22, 2014 at 11:29

2 Answers 2

0

In your ajax request you can make use of crossDomain option in order to enable CORS

$.ajax({
    async : true,
    type:'GET',
    contentType: 'application/json',
    url: url,
    cache:false,
    dataType: 'json',
    crossDomain:true,
    headers: {
        "token":"123"
    },

    success: function(data) { 
        alert("Cadastrado com sucesso!", data);
    },



 error: function(data) {
        alert("Erro!", data);
    }
});

If you working on a directory system then you need to disable the chrome security and allow CORS validation on your local system. To do this go to your chrome.exe folder (I am talking about windows over here) and execute this command:

chrome.exe --disable-web-security
1
  • So the problem was that I add the missing Java CORS filter an Access-Control-Allow-Headers with the word token, I'm going through in my headers in AJAX, I'll put the answer below. Thank you
    – Pedro HOS
    Aug 22, 2014 at 12:49
0

The solution to the problem was to add word token:

  responseContext.getHeaders().add("Access-Control-Allow-Headers", "token, origin, content-type, accept, authorization");

CORS in my filter in Java, since I'm using it in the headers AJAX. The complete solution is this:

@Provider
public class ConfigureCORSFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext,ContainerResponseContext responseContext) throws IOException {
          responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
          responseContext.getHeaders().add("Access-Control-Allow-Headers", "token, origin, content-type, accept, authorization");
          responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
          responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
          responseContext.getHeaders().add("Access-Control-Max-Age", "1209600");
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.