title: Ajax request with JAX-RS/RESTEasy implementing CORS
body: <p>I have two servers (Apache and JBoss AS7) and I need to provide access to all http methods to a client. All these request must be sent via ajax.
  Example of the client code:</p>
  
  <pre><code>$.ajax({
        type: "get",
        url: "http://localhost:9080/myproject/services/mobile/list",
        crossDomain: true,
        cache: false,
        dataType: "json",
        success: function(response) {
          console.log(response);
        },
        error: function (jqXHR, textStatus, errorThrown) {
          console.log(textStatus);
          console.log(jqXHR.responseText);
          console.log(errorThrown);
          }
      });
  </code></pre>
  
  <p>In JBoss AS7 I'm using RESTEasy, implementing CORS as follows:</p>
  
  <pre><code>@Path("/mobile")
  @Provider
  @ServerInterceptor
  public class GroupMobile implements MessageBodyWriterInterceptor {
  
  @Inject
  private GroupDAO groupDAO;
  
  @GET
  @Path("/list")
  @Produces(MediaType.APPLICATION_JSON)
  public List&lt;Group&gt; getGroups() {
      return groupDAO.listAll();
  }
  
  @Override
  public void write(MessageBodyWriterContext context) throws IOException,
          WebApplicationException {
      context.getHeaders().add("Access-Control-Allow-Origin", "*");
      context.proceed();
  }
  
  @OPTIONS
  @Path("/{path:.*}")
  public Response handleCORSRequest(
          @HeaderParam("Access-Control-Request-Method") final String requestMethod,
          @HeaderParam("Access-Control-Request-Headers") final String requestHeaders) {
      final ResponseBuilder retValue = Response.ok();
  
      if (requestHeaders != null)
          retValue.header("Access-Control-Allow-Headers", requestHeaders);
  
      if (requestMethod != null)
          retValue.header("Access-Control-Allow-Methods", requestMethod);
  
      retValue.header("Access-Control-Allow-Origin", "*");
  
      return retValue.build();
  }
  }
  </code></pre>
  
  <p>web.xml and beans.xml are empty files.
  When I access MyIP:8080 (Apache), I get the error message:</p>
  
  <pre><code>XMLHttpRequest cannot load http://localhost:9080/myproject/services/mobile/list?_=1359480354190. Origin http://MyIP:8080 is not allowed by Access-Control-Allow-Origin.
  </code></pre>
  
  <p>Does anybody know what is wrong?</p>