我正在使用 Angular (10) 应用程序通过 REST api 访问 Java (1.7) 应用程序。一切都按预期工作。有许多 GET 和 POST 方法调用可以正常工作。
问题
我添加了一个 PUT 方法。现在,当 Angular 应用程序调用 PUT 方法时,会出现以下错误:
<块引用>被 cors 策略阻止的方法没有 'access-control-allow-origin' 标头 存在于请求的资源上
问题
这适用于 GET 和 POST 方法。我如何让它也适用于 PUT 方法?
更多信息
在 Java 应用程序中,我确实设置了以下内容:
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, HEAD, OPTIONS");
这是 PUT 方法:
@PUT
@Path("/request/{companyId}/{orderNumber}")
@ApiOperation(value="description")
@ApiResponses({
@ApiResponse(code=200, message="Success"),
@ApiResponse(code=404, message="Not Found")
})
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public Response updateSubmittedTrip(@HeaderParam("Authorization") String token, @ApiParam(required = true) TravelRequisitionFormDTO travelRequisitionFormDTOBody, @PathParam("companyId") String companyId, @PathParam("orderNumber") String orderNumber, @Context HttpServletRequest request) {
这里是POST方法:
@POST
@Path("/request/{companyId}/{orderNumber}")
@ApiOperation(value="description")
@ApiResponses({
@ApiResponse(code=200, message="Success"),
@ApiResponse(code=404, message="Not Found")
})
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubmitTrip(@HeaderParam("Authorization") String token, @ApiParam(required = true) TravelRequisitionFormDTO travelRequisitionFormDTOBody, @PathParam("companyId") String companyId, @PathParam("orderNumber") String orderNumber, @Context HttpServletRequest request) {
网络
更新
我将 PUT 更改为 POST,但仍然出现错误。所以错误不在于 PUT 方法。那么问题一定出在端点上。
我也把Angular客户端改成从另一个地方调用PUT方法,也没有得到CORS错误。所以这对我来说表明问题在于客户端如何调用端点(即客户端问题)。