[英]Access-Control-Allow-Origin Errors even though the REST Server sets header
[英]No 'Access-Control-Allow-Origin' error even though the header is there on my server side code
更新:由於某種原因,服務器端腳本具有Unicode字符,當我通過記事本保存它時,將其切換為ANSII並可以正常工作。 我不確定Unicode東西是如何進入那里的,但是現在它可以工作了。 希望這篇文章將來對某人(很有可能是我)有所幫助。
盡管我具有正確的標頭,但仍收到CORS錯誤。 截至一個月前,它一直在工作,並且服務器或客戶端代碼沒有任何更改。
服務器端
<?php
header('Access-Control-Allow-Origin: https://mywebsite.com');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
header('Access-Control-Allow-Credentials: true');
客戶端錯誤
XMLHttpRequest cannot load https://serverside.com/serversidecode.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://mywebsite.com' is therefore not allowed access.
服務器端代碼完全運行,但不會將成功數據發送回我的客戶端服務器。
這可能能夠幫助您使用php標頭進行CORS
“正確處理CORS請求要花些時間。這是一個可以更充分(正確)響應的功能。”
**
* An example CORS-compliant method. It will allow any GET, POST, or OPTIONS requests from any
* origin.
*
* In a production environment, you probably want to be more restrictive, but this gives you
* the general idea of what is involved. For the nitty-gritty low-down, read:
*
* - https://developer.mozilla.org/en/HTTP_access_control
* - http://www.w3.org/TR/cors/
*
*/
function cors() {
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// 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, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
echo "You have CORS!";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.