我在 Mulesoft 中创建了一个 API,我需要从 Angular 前端使用它。GET 请求工作正常,但是当我尝试发送 POST/DELETE 请求时,它在发送 OPTIONS 请求时出错(响应没有 cors 标头,而 cors 设置正确)。当我在特定路由上添加我的一个端点以接受 OPTIONS 时,它工作正常。我可以以某种方式为在任何路由上发送的每个 OPTIONS 请求创建一个全局侦听器。HTTP 连接器代码如下。
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="8d79948b-c780-43eb-b7d1-41235210a0ff" basePath="/api" >
<http:listener-connection host="localhost" port="8081" protocol="HTTP" />
<http:listener-interceptors >
<http:cors-interceptor >
<http:origins >
<http:origin url="http://localhost:4200" accessControlMaxAge="30000">
<http:allowed-methods>
<http:method methodName="GET" />
<http:method methodName="POST" />
<http:method methodName="DELETE" />
<http:method methodName="OPTIONS" />
<http:method methodName="PUT" />
</http:allowed-methods>
<http:allowed-headers >
<http:header headerName="Content-Type" />
<http:header headerName="Access-Control-Allow-Headers" />
<http:header headerName="Access-Control-Allow-Origin" />
</http:allowed-headers>
<http:expose-headers />
</http:origin>
</http:origins>
</http:cors-interceptor>
</http:listener-interceptors>
</http:listener-config>