【问题标题】:How to remove authorization header from request [duplicate]如何从请求中删除授权标头[重复]
【发布时间】:2016-12-12 11:51:40
【问题描述】:

我使用MEAN Stack User Registration and Login Example & Tutorial 作为我的应用程序的基础。它为 run 函数中的每个请求添加一个 auth 标头:

$http.defaults.headers.common['Authorization'] = 'Bearer ' + $window.jwtToken;

我想将图像上传到 Cloudinary,但出现此错误:

XMLHttpRequest 无法加载 https://api.cloudinary.com/v1_1/xxxx/upload。预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段授权。

如何专门针对 Cloudinary 的请求删除此标头?

【问题讨论】:

    标签: angularjs jwt cloudinary


    【解决方案1】:

    您将需要一个拦截器来检查请求的 url,如果匹配则清除标头。或者,您可以使用$http 配置参数。

    使用参数:

    $http.post('https://api.cloudinary.com/v1_1/' + someId + '/upload', data, { headers: {} });
    

    使用拦截器:

    .factory('cloudinaryInterceptor', function() {
     return {
      request: function(config){
       var authHeader = config.headers('authorization');
       //Check for the host
       var regex = /api\.cloudinary\.com/i;
       if(regex.test(config.url))
        //Detach the header
        delete config.headers.authorization;
       return config;
      }
     }
    });
    

    记得在配置阶段推送拦截器

    $httpProvider.interceptors.push('cloudinaryInterceptor');
    

    【讨论】:

      【解决方案2】:

      以前有人问过这个问题。答案可以在here找到。

      当您开始使用自定义请求标头时,您将获得 CORS 预检。这是一个使用 HTTP OPTIONS 动词并包含多个标头的请求,其中一个是 Access-Control-Request-Headers,列出了客户端希望在请求中包含的标头。

      您需要使用适当的 CORS 回复该 CORS 预检 标题使这项工作。其中之一确实是 访问控制允许标头。该标题需要包含相同的 值包含(或更多)的 Access-Control-Request-Headers 标头。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-12
        • 1970-01-01
        • 2015-01-20
        • 2019-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多