如何向$window.open添加身份验证头

时间:2022-03-21 06:45:17

I have angularjs application where users enter data that is saved to database, then on server side it is compiled into pdf file. All access requires appropriate authentication headers in place. After needed data is filled a user presses button to save data and then to retrieve pdf file. Optimally, I call $window.open(url_generating_pdf) in my angularjs app. This works well and opens in another window, but how to add authentication header to this $window request? In my understanding I cannot download pdf, and print it with ajax, so I am missing this authentication. Or would there be other ways to call url from server, and make the file open in another window?

我有一个angularjs应用程序,用户输入保存到数据库的数据,然后在服务器端编译成pdf文件。所有访问都需要适当的身份验证头。填写所需数据后,用户按下按钮保存数据,然后检索pdf文件。在我的angularjs应用程序中,我调用$window.open(url_generating_pdf)。根据我的理解,我不能下载pdf,并使用ajax打印它,所以我没有这个身份验证。或者是否有其他方法从服务器调用url,并使文件在另一个窗口中打开?

1 个解决方案

#1


6  

I think you can add this authentication parameters in URL and do a GET in your server side

我认为您可以在URL中添加此身份验证参数,并在服务器端执行GET操作

//Add authentication headers as params
var params = {
    access_token: 'An access_token',
    other_header: 'other_header'
};

//Add authentication headers in URL
var url = [url_generating_pdf, $.param(params)].join('?');

//Open window
window.open(url);

#1


6  

I think you can add this authentication parameters in URL and do a GET in your server side

我认为您可以在URL中添加此身份验证参数,并在服务器端执行GET操作

//Add authentication headers as params
var params = {
    access_token: 'An access_token',
    other_header: 'other_header'
};

//Add authentication headers in URL
var url = [url_generating_pdf, $.param(params)].join('?');

//Open window
window.open(url);