1

我在 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>
4

1 回答 1

0

这是我的配置,您需要添加公开标头:

<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>
于 2020-06-13T03:18:43.820 回答