has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. chun
时间: 2023-10-29 17:57:54 浏览: 100
corsyusha:简单快速的代理,可在针对现有API进行原型设计时绕过CORS问题,而不必担心CORS
CORS stands for Cross-Origin Resource Sharing, and it is a security mechanism implemented by web browsers to protect users from malicious scripts. When a web page makes a request to a different domain, the browser checks if the server includes the 'Access-Control-Allow-Origin' header in its response. If the header is not present or does not match the origin of the requesting page, the browser blocks the request.
The error message you mentioned, "has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource," indicates that the server you are making a request to does not allow cross-origin requests from your domain. This can happen if the server is not configured properly or intentionally restricts access.
To fix this issue, you have a few options:
1. Modify the server's response headers: If you have control over the server, you can add the 'Access-Control-Allow-Origin' header to the response. This header should include the domain from which you are making the request or use "*" to allow requests from any domain. Here is an example of how to set this header in a server response:
```
Access-Control-Allow-Origin: https://your-domain.com
```
2. Use a proxy server: If you do not have control over the server's configuration or cannot modify it, you can set up a proxy server that acts as an intermediary between your web page and the target server. The proxy server can make the request on behalf of your page, and since the request will be from the same origin as your page, it will not be blocked by CORS restrictions.
3. Utilize JSONP or CORS workaround techniques: JSONP (JSON with Padding) is an older technique that allows cross-origin requests by loading scripts instead of making XMLHttpRequests. However, it has some limitations and potential security risks. Alternatively, some servers may provide specific workarounds or APIs that bypass CORS restrictions. You can check the server's documentation or contact the server owner to see if there are any alternative methods available for making cross-origin requests.
Remember, the specific solution will depend on your server's configuration and the requirements of your application.
阅读全文