如何在应用程序级别的 Mulesoft 中启用 CORS?

How to enable CORS in Mulesoft on application level?

我需要在应用程序级别的 Mulesoft 中启用 CORS。我已经通过在侦听器上设置 CORS 找到了一些解决方案,但它对我不起作用。这是我的 HTTP 侦听器配置:

<http:listener-config name="erp-apoteka-api-httpListenerConfig">
    <http:listener-connection host="0.0.0.0" port="8081" protocol="HTTPS"/>
    <http:listener-interceptors>
        <http:cors-interceptor>
            <http:origins>
                <http:origin url="http://localhost:3000" 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>

我在前端使用 React 和 Axios,当我进行一些调用时,出现此错误:

Access to XMLHttpRequest at 'http://localhost:8081/api/kategorije' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
KategorijeContainer.tsx:53 Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:83)
xhr.js:178 GET http://localhost:8081/api/kategorije net::ERR_FAILED

我的 axios 设置是这样的:

export const axiosInstance = axios.create({
baseURL: BaseRoutes.ApiUrl,
headers: {
    'Content-Type': 'application/json',
},
responseType: 'json',

});

您的 http:listener-连接协议设置为 HTTPS,但您的来源来自 http://localhost..我假设您仍处于开发阶段并且没有在您的设备上启用 TLS mule 后端,尝试将其更改为 HTTP。