2015-07-21 139 views
0

我希望我的应用程序向我的后端发送请求,并且我需要的是基本身份验证。我有以下代码:

myApp.factory('ApiRequest', ['$http', 'AccountService', function($http, AccountService) { 

var apiURL = 'https://web.appspot.com/_ah/api/server'; 
var apiVer = 'v1'; 
var url = apiURL + '/' + apiVer; 

return { 
    getMessages: function(time_offset) { 
     var encodedCredentials = AccountService.generateBase64Credentials(); 

     // $http.defaults.headers.common['Authorization'] = encodedCredentials; tried like this, but doesn't work too 
     return $http.get(
      url + '/message', { 
       headers: { 
        'Authorization': AccountService.generateBase64Credentials() 
       }, 
       params: { 
        recipient_id: '6305746161500160', 
        recipient_type: 'circle', 
        time_offset: time_offset ? time_offset : null 
       } 
      } 
     ) 
    } 
} 
}]); 

可惜每次我获得此授权头时间:

Basic W29iamVjdCBPYmplY3RdOltvYmplY3QgT2JqZWN0XQ== 

我的功能AccountService.generateBase64Credentials返回字符串:基本window.btoa(账号:密码),为何还以这种方式工作?我怎样才能使它正常工作

回答

0

您输入Base64编码是不正确的。当我解码字符串时,它返回“[object Object]:[object Object]”(不包括双引号)。请先检查输入到“window.btoa”函数。

+0

我知道解码的结果,但我不知道它可能是问题所在。谢谢! – SmiglowiecX