Shanaka
Shanaka

Reputation: 1738

Access-Control-Allow-Origin

Hi I'm developing a web application using a REST web service as server side. When I make a call to rest web service from my client side it gives me the following error. Servie is running uder http://localhost:8010/service and client is running under http://localhost:8020/client. I have deployed this in jetty.

XMLHttpRequest cannot load http://localhost:8010/Service/rest/employee/basicupdate. Origin http://localhost:8020 is not allowed by Access-Control-Allow-Origin.

Below I have included my rest method

@POST
@Path("/basicupdate")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Context 
public String isValidLogin(@FormParam("employeeId") int employeeId, @FormParam("firstName") String firstName, @FormParam("lastName") String lastName, @FormParam("gender") String gender, @FormParam("dob") String dob ) throws JsonGenerationException, JsonMappingException, IOException{
    Response response = new Response();
    String responseString = "";
    try {

        //Application logic

        response.setCode(MessageCode.SUCCESS);
        response.setMessage("Successfully Updated");

    } catch (CustomException e) {
        response.setCode(MessageCode.ERROR);
        response.setMessage(e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        response.setCode(MessageCode.ERROR);
        response.setMessage(e.getMessage());
        e.printStackTrace();
    }finally{
        responseString = mapper.writeValueAsString(response);
    }   
    return responseString;
}

Upvotes: 0

Views: 3402

Answers (1)

<web-app>
 <filter>
   <filter-name>cross-origin</filter-name>
   <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
   <init-param>
       <param-name>allowedOrigins</param-name>
       <param-value>*</param-value>
   </init-param>
   <init-param>
       <param-name>allowedMethods</param-name>
       <param-value>*</param-value>
   </init-param>
   <init-param>
       <param-name>allowedHeaders</param-name>
       <param-value>*</param-value>
   </init-param>
 </filter>
 <filter-mapping>
     <filter-name>cross-origin</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

Upvotes: 1

Related Questions