如何从请求中删除授权标头

Mr *_*ith 1 jwt angularjs cloudinary

我正在使用MEAN Stack用户注册和登录示例和教程作为我的应用程序的基础.它为run函数中的每个请求添加了一个auth标头:

$http.defaults.headers.common['Authorization'] = 'Bearer ' + $window.jwtToken;
Run Code Online (Sandbox Code Playgroud)

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

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

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

Mul*_*ary 9

您将需要一个拦截器来检查请求的url并在匹配时清除标头.或者你可以使用$httpconfig参数.

使用参数:

$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;
  }
 }
});
Run Code Online (Sandbox Code Playgroud)

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

$httpProvider.interceptors.push('cloudinaryInterceptor');
Run Code Online (Sandbox Code Playgroud)