cors error php
时间: 2023-09-08 22:15:19 浏览: 105
CORS (Cross-Origin Resource Sharing) error occurs when a web page running on one domain tries to access resources from another domain. This error typically occurs when making AJAX requests from a web page hosted on one domain to a PHP script hosted on another domain.
To resolve the CORS error in PHP, you can add the following code to your PHP script:
```
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
```
This code adds the necessary CORS headers to the response, allowing the web page to access the resources from the PHP script. Additionally, you can also configure your web server to allow CORS by adding the necessary headers to the response using the .htaccess file.
阅读全文