from origin 'http://localhost:8081' has been blocked by CORS policy
时间: 2023-09-09 11:09:32 浏览: 121
The CORS (Cross-Origin Resource Sharing) policy is a security measure implemented by web browsers to restrict access to resources (such as APIs) on a different origin (domain, protocol, or port) than the one the web page is served from.
In your case, it seems that you are making a request from 'http://localhost:8081' to a different origin, and the server is blocking that request due to CORS policy restrictions. To resolve this issue, you can try the following solutions:
1. Enable CORS on the server-side: If you have control over the server-side code, you can configure the server to include the appropriate CORS headers in the response. These headers allow the browser to determine if it should allow the request. The minimal required headers are 'Access-Control-Allow-Origin' and 'Access-Control-Allow-Methods'. You can set the value of 'Access-Control-Allow-Origin' to '*' to allow requests from any origin during development. However, in production, it's recommended to set it to the specific origin of your application.
2. Use a proxy server: If you don't have control over the server-side code or enabling CORS is not feasible, you can set up a proxy server that acts as an intermediary between your client-side code and the server. Your client-side code will make requests to the proxy server, and the proxy server will forward those requests to the actual server. Since the proxy server will be on the same origin as your client-side code, you won't face any CORS issues.
3. Disable CORS in the browser: Although not recommended for production environments, during development, you can disable CORS checks in your browser. Keep in mind that this is a temporary solution and should not be used in a production environment. The method to disable CORS varies between browsers, so you need to search for instructions specific to your browser.
Remember to consider security implications when implementing any solution and ensure that proper CORS configurations are applied in a production environment.
阅读全文