gpt4 book ai didi

java - 访问控制允许来源

转载 作者:行者123 更新时间:2023-12-01 14:58:06 26 4
gpt4 key购买 nike

您好,我正在开发一个使用 REST Web 服务作为服务器端的 Web 应用程序。当我从客户端调用 REST Web 服务时,出现以下错误。 Servie 在 http://localhost:8010/service 下运行,客户端在 http://localhost:8020/client 下运行。我已经在 jetty 部署了这个。

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

最佳答案

<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>

关于java - 访问控制允许来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14083481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com