我在Mulesoft中创建了一个API,我需要从Angular前端中使用它。 GET请求工作正常,但是当我尝试发送POST / DELETE请求时,在发送OPTIONS请求时会出错(响应没有cors标头,而cors设置正确)。当我在证书路由上添加我的一个端点以接受选项时,它可以正常工作。我能以某种方式为在任何路由上发送的每个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>
这是我的配置,您需要添加公开标题:
<http:listener-config name="agentportal-httpListenerConfig">
<http:listener-connection host="0.0.0.0" port="8081" />
<http:listener-interceptors >
<http:cors-interceptor allowCredentials="false">
<http:origins >
<http:origin url="http://localhost:4200" accessControlMaxAge="30000" >
<http:allowed-methods >
<http:method methodName="OPTIONS" />
<http:method methodName="GET" />
<http:method methodName="POST" />
</http:allowed-methods>
<http:allowed-headers >
<http:header headerName="Content-Type" />
<http:header headerName="Authorization" />
<http:header headerName="Accept" />
</http:allowed-headers>
<http:expose-headers >
<http:header headerName="Access-Control-Allow-Origin" />
<http:header headerName="Access-Control-Allow-Methods" />
<http:header headerName="Access-Control-Allow-Headers" />
</http:expose-headers>
</http:origin>
</http:origins>
</http:cors-interceptor>
</http:listener-interceptors>
</http:listener-config>