【问题标题】:Auth headers getting removed when requesting absolute url请求绝对 url 时,身份验证标头被删除
【发布时间】:2015-04-30 01:45:04
【问题描述】:

当我尝试向本地 url 发出请求时,一切正常。但是,当我将 url 更改为我们的测试服务器的实际 url(以 http 为前缀)时,http auth 标头似乎已从请求中剥离。 评论的 $http... 有效。但下面的没有。 有人知道为什么会这样吗?

function OneCitizensList($scope, $http) {
   var username = "user",
      password = "pass";

   $scope.citizen = [];
   $scope.medication = [];

// $http.get('/mobile/unity/patient/?patientId=36', {
   $http.get('http://something:8083/unity/medication/list?patientId=102', {
   headers: {
       'Authorization': "Basic " + Base64Encoder(username + ':' + password)
     }
     }).
      success(function(data, status, headers, config) {
         console.log(data);

      }).
      error(function(data, status, headers, config) {
         console.log("Error");
      });
}

【问题讨论】:

    标签: angularjs restful-authentication


    【解决方案1】:

    我认为您的 headers 对象中缺少一个括号。

    $http.get('http://something:8083/unity/medication/list?patientId=102', {
       headers: {
           {'Authorization': "Basic " + Base64Encoder(username + ':' + password)}
         }
         }).
          success(function(data, status, headers, config) {
             console.log(data);
    
          }).
          error(function(data, status, headers, config) {
             console.log("Error");
          });
    

    或完整的方法(不是速记):

    $http({method: 'GET', url: 'http://something:8083/unity/medication/list?patientId=102', headers: {
        'Authorization': "Basic " + Base64Encoder(username + ':' + password)}
    });
    

    还要确保您的服务器允许 CORS 请求。

    为了使基本身份验证正常工作,您还需要在 $http 配置中使用 withCredentials: true

    例子:

    $http.get('https://yourURL.test', { headers: {your_headers}, withCredentials: true});
    

    【讨论】:

    • 它不适用于额外的 {}。而且,它适用于本地 url。只是当我使用远程 url 时,似乎 auth 标头已从请求中剥离,如 chrome 开发控制台中所示。
    猜你喜欢
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2020-05-06
    • 1970-01-01
    相关资源
    最近更新 更多