場景:我的本地網頁服務器無法訪問本地的接口服務器接口提示一下錯誤:大致意思是:是一個跨域請求我的沒有訪問該地址的權限(接口服務器采用的是PHP編寫)
XMLHttpRequest cannot load http://localhost/mz/goods/getList. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access.
解決方案:
header('Access-Control-Allow-Origin: *'); header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); header('Access-Control-Allow-Methods: GET, POST, PUT');
為ajaxReturn 方法設置允許跨域訪問的請求頭。
參考地址:http://blog.sina.com.cn/s/blog_6751f30b0102w2x1.html,http://stackoverflow.com/questions/20433655/no-access-control-allow-origin-header-is-present-on-the-requested-resource-or
thinkphp 5版本解決
情景:
uni-app使用vue框架開發混合APP,雖然APP或者小程序沒有跨域,但希望就是寫完這個既有H5,又有APP,小程序等,所以能通過后端解決跨域最好。但是不知道是vue的原因還是什么,在PHP接口基類中添加了header頭完全不起作用。官方給出的方法也有,具體可以看https://uniapp.dcloud.io/api/request/request。
分析:
1. 以前的做法是在接口添加以下部分就可以解決ajax的跨域(雖然也用過jsonp)。
// 指定允許其他域名訪問 // header('Access-Control-Allow-Origin:*'); // // 響應類型 // header('Access-Control-Allow-Methods:*'); // // 響應頭設置 // header('Access-Control-Allow-Headers:*');
2. 添加后請求,報錯“Access to XMLHttpRequest at 'http://www.unxxx.com/api/v1.user/login' from origin 'http://192.168.2.121:8000' has been blocked by CORS policy: Request header field token is not allowed by Access-Control-Allow-Headers in preflight response.”;自定義的請求頭token不被允許。因為接口請求需要帶上token,把token放在自定義請求頭上再傳到PHP。
3. 於是就將token改為普通參數方式傳遞,但依然報錯,Access to XMLHttpRequest at 'http://www.xxxxxx.com/api/v1.user/login' from origin 'http://192.168.2.121:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource。
4. 以上可以看出就是普通跨域報錯了,然后看一下瀏覽器的請求響應報文。
5. 發現返回過來的頭部信息完全不是自己在接口上指定的,抱着試一試的念頭,把跨域請求放到了TP5.1的入口文件\public\index.php,竟然就可以正常請求了,目前我也不清楚原因是什么。
ini_set('session.cookie_domain',"*.qiema.cc");//跨域訪問Session $url = $_SERVER["HTTP_REFERER"]; //獲取完整的來路URL $http = 'http://';$str=''; if (strstr($url, $http)){ $str = str_replace("http://","",$url); //去掉http:// } else { $str = str_replace("https://","",$url); //去掉https:// $http = 'https://'; } $strdomain = explode("/",$str); // 以"/"分開成數組 $domain = $strdomain[0]; //取第一個“/”以前的字符 $origin = request()->header('Origin'); if (!$origin) $origin = 'jifen.c.qiema.cc'; header('Access-Control-Allow-Origin:'.$origin); header('Access-Control-Allow-Credentials:true'); header('Access-Control-Allow-Methods:GET,POST,OPTIONS'); header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); header('Access-Control-Allow-Headers: content-type,token,dealerauth,mstoreauth,Authorization'); // 設置允許自定義請求頭的字段
ini_set('session.cookie_domain',"*.qiema.cc");//跨域訪問Session$url = $_SERVER["HTTP_REFERER"]; //獲取完整的來路URL$http = 'http://';$str='';if (strstr($url, $http)){ $str = str_replace("http://","",$url); //去掉http://} else { $str = str_replace("https://","",$url); //去掉https:// $http = 'https://';}$strdomain = explode("/",$str); // 以"/"分開成數組$domain = $strdomain[0]; //取第一個“/”以前的字符$origin = request()->header('Origin');if (!$origin) $origin = 'jifen.c.qiema.cc';header('Access-Control-Allow-Origin:'.$origin);header('Access-Control-Allow-Credentials:true');header('Access-Control-Allow-Methods:GET,POST,OPTIONS');header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');header('Access-Control-Allow-Headers: content-type,token,dealerauth,mstoreauth,Authorization'); // 設置允許自定義請求頭的字段