-2

i'am developing a REST api and consumes it via my browser(firefox), but i'm getting this error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/siga/av. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

i'm using embedded jetty 9 for deploiement.

My methode:

@GET
@Path("av")
@Produces(MediaType.TEXT_PLAIN)
@Override
public Response avoidCrossDomain() {
    return Response.status(Status.FOUND).entity("hello").header("Access-Control-Allow-Origin", "*").build();
}   

Postman Response :

Access-Control-Allow-Origin →*

Content-Length →5

Content-Type →text/plain

Date →Mon, 29 Jan 2018 07:58:28 GMT

Server →Jetty(9.2.3.v20140905)

Javascript

$.ajax({

url: 'http://localhost:8080/siga/av',
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
success: function(data) { alert("data"); },
error: function() { alert('Failed!'); },
});
Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26

1 Answers1

0

You have to define the CORS filter for jetty in the web.xml:

<filter>
    <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

You can find further information concerning the CORS filter in the jetty documentation

Georg Leber
  • 3,470
  • 5
  • 40
  • 63