我的API返回一个PDF文档,我需要对用户进行身份验证。在我的AngularJS应用程序中,用户单击一个调用函数的链接:
 <a ng-click="getBankDetails()">Print bank details</a>控制器:
$scope.getBankDetails = ->
$http.get(ENV.apiEndpoint + "/api/v1/users/get_bank_details", { headers: { 'Accept': 'application/pdf' }, responseType: 'arraybuffer'  })
   .success((resp) ->
     file = new Blob([resp], {type: 'application/pdf'})
     fileURL = URL.createObjectURL(file)
     window.open(file);
   )但这不管用。
我已经尝试删除API上的身份验证需求。上述方法仍然不起作用,但如果我这样做了
window.open(ENV.apiEndpoint + "/api/v1/users/get_bank_details")这很管用。除了确认我的API运行良好外,没有什么特别的帮助。
我正在使用ng-令牌-auth作为认证
它储存在饼干里,因为我不在这里使用科多瓦
$authProvider.configure
  apiUrl: ENV.apiEndpoint
  storage: if _.isUndefined(window.cordova) then "cookies" else "localStorage"任何帮助都将不胜感激。
发布于 2018-02-16 16:02:22
如果您使用的是基于令牌的auth,我将尽可能简单地将authToken添加到URL中以接收文件。通过后端将authToken处理为GET-Param:
$scope.getBankDetails = function () {
 window.open(ENV.apiEndpoint + "/api/v1/users/get_bank_details?authToken=XYZ");
}https://stackoverflow.com/questions/48829837
复制相似问题