Php / AngularJs / Ionic Request标头字段不允许授权

时间:2022-08-22 19:39:46

First of all I want to say that I tried All * solutions and no one works for me :(

首先,我想说我尝试了所有*解决方案,没有人适合我:(

In my server config I have got this

在我的服务器配置中,我有这个

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;
}

And in my application I have been config the $httpProvider.

在我的应用程序中,我已经配置了$ 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";
});

The service call ( Headers with token have been set in the $httpProvider.interceptors )

服务调用(带有令牌的标头已在$ httpProvider.interceptors中设置)

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

The question is..... I don't know why but in my mobile phone ( Android or iOS ) works fine but... in my web browser not. I tried in chrome, mozilla and safari. If I install the Chrome plugin CORS works fine if not always returns that error

问题是.....我不知道为什么,但在我的手机(Android或iOS)工作正常,但...在我的网络浏览器中没有。我尝试过chrome,mozilla和safari。如果我安装Chrome插件,CORS可以正常工作,如果不总是返回该错误

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

无法加载资源:请求标头字段Access-Control-Allow-Headers不允许授权。

XMLHttpRequest cannot load http://myurl. Request header field Authorization is not allowed by Access-Control-Allow-Headers.

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

How can I fix that in my browser without CORS plugin?

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

Thanks to all who want to help :)

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

1 个解决方案

#1


1  

I think it's not server issue please check this link:

我认为这不是服务器问题请查看此链接:

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

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

Then this link:

然后这个链接:

api calling error in ionic-framework

离子框架中的api调用错误

#1


1  

I think it's not server issue please check this link:

我认为这不是服务器问题请查看此链接:

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

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

Then this link:

然后这个链接:

api calling error in ionic-framework

离子框架中的api调用错误