我需要在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-connection协议被设置为HTTPS,但你的来源是来自于 http:/localhost.. 我假设你还在开发阶段,并且没有在你的骡子后台启用TLS,试着改成HTTP。