Php / AngularJs / Ionic请求头字段不允许授权


Php / AngularJs / Ionic Request header field Authorization is not allowed

首先我想说的是,我尝试了所有的stackoverflow解决方案,没有一个为我工作:(

在我的服务器配置中,我得到了这个

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
// respond to preflights
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
// return only the headers and not the content
// only allow CORS if we're doing a GET - i.e. no saving for now.
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'GET') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
}
exit;
}

在我的应用程序中,我已经配置了$httpProvider。

.config(['$httpProvider',
    function($httpProvider) {
        function tokenInjector($localStorage) {
             return {
                 request: function(config) {
                     if ($localStorage.token) {
                         config.headers.authorization = 'Basic ' + $localStorage.token;
                     } else {
                         delete config.headers.authorization;
                     }
                     return config;
                 }
             };
        }
        $httpProvider.interceptors.push(tokenInjector);
    }
])
.config(function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = false;
    delete $httpProvider.defaults.headers.common["X-Requested-With"];
    $httpProvider.defaults.headers.common["Accept"] = "application/json";
    $httpProvider.defaults.headers.common["Content-Type"] = "application/json";
});

服务调用(Headers with token已经在$httpProvider.interceptors中设置)

$http.get('http://myurl')

问题是.....我不知道为什么,但我的手机(安卓或iOS)工作得很好,但是……在我的浏览器中没有。我试用了chrome、mozilla和safari。如果我安装Chrome插件CORS工作良好,如果不总是返回错误

Failed to load resource: Request header field Authorization is not allowed by Access-Control-Allow-Headers.

XMLHttpRequest无法加载http://myurl。请求不允许授权Access-Control-Allow-Headers .

如何在没有CORS插件的浏览器中修复该问题?

感谢所有想要帮助的人:)

我认为这不是服务器的问题,请检查这个链接:

http://blog.ionic.io/handling-cors-issues-in-ionic/

那么这个链接:

ionic-framework中api调用错误