Cross-Origin Resource Sharing (CORS) ,定义了在跨域访问资源时浏览器和服务器之间如何通信。CORS背后的基本思想是使用自定义的HTTP头部允许浏览器和服务器相互了解对方,从而决定请求或响应成功与否。
解决方法:
①后端加响应头
header("Access-Control-Allow-Origin: *"); header(‘Access-Control-Allow-Credentials: true‘); header(‘Access-Control-Max-Age: 86400‘); // cache for 1 day // header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); // Access-Control headers are received during OPTIONS requests if ($_SERVER[‘REQUEST_METHOD‘] == ‘OPTIONS‘) { if (isset($_SERVER[‘HTTP_ACCESS_CONTROL_REQUEST_METHOD‘])) header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); if (isset($_SERVER[‘HTTP_ACCESS_CONTROL_REQUEST_HEADERS‘])) header("Access-Control-Allow-Headers: {$_SERVER[‘HTTP_ACCESS_CONTROL_REQUEST_HEADERS‘]}"); }
②设置前端ajax请求参数
时间: 2024-10-12 02:18:21