访问控制允许来源

时间:2012-12-29 16:09:29

标签: java jquery cross-domain

您好我正在使用REST Web服务作为服务器端开发Web应用程序。当我从客户端打电话给休息Web服务时,它给了我以下错误。 Servie正在运行uder http://localhost:8010/service,客户端正在http://localhost:8020/client下运行。我在码头部署了这个。

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

下面我已经包含了我的休息方法

@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;
}

1 个答案:

答案 0 :(得分: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>