AngularJS基本认证

时间:2014-03-20 12:11:57

标签: angularjs basic-authentication http-basic-authentication

为什么我会收到401?凭证是正确的。

  app.config(['$httpProvider', function($httpProvider) {  
      $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
      $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
      $httpProvider.defaults.headers.common.Authorization = 'Basic ' + btoa("xxx:yyy");
      $httpProvider.defaults.headers.common.withCredentials = true;
  }

...

 $http({
    method: "POST",
    url: url
    data: data,
})

1 个答案:

答案 0 :(得分:2)

尝试在Authorization内添加$http标题,如:

 $http({
    method: "POST",
    url: url
    data: data,
    headers: {'Authorization': 'Basic ' + btoa("xxx:yyy")},
})