window.open with headers
Can I control the HTTP headers sent by window.open (cross browser)?
No
If not, can I somehow window.open a page that then issues my request with custom headers inside its popped-up window?
- You can request a URL that triggers a server side program which makes the request with arbitrary headers and then returns the response
- You can run JavaScript (probably saying goodbye to Progressive Enhancement) that uses XHR to make the request with arbitrary headers (assuming the URL fits within the Same Origin Policy) and then process the result in JS.
I need some cunning hacks...
It might help if you described the problem instead of asking if possible solutions would work.
How to add request headers as array to window.open post url in javascript?
You cannot use window.open to do POST request to the backend service, what you could do would be use fetch function
fetch(url, {
method: "POST",
body: JSON.stringify({"reporttype" : [1,2,3,4,5]}),
headers: {"Content-Type": "application/json"}
.then(response => response.json())
.then(data => console.log(data));
You could also try using XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
console.log(xhr.response);
}
}
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.send(JSON.stringify({"reporttype" : [1,2,3,4,5]}));
$window.open with context / headers
Download the file first, then open it:
var url = this.apiBaseUrl + "/exportToExcel/" + id;
var headers = {
//Put headers here
};
var config = {
responseType: 'blob',
headers: headers
};
$http.get(url, config).then(function (response) {
var blob = response.data;
var u = URL.createObjectURL(blob);
window.open(u,"_blank");
});
This will GET the file as a blob, convert it to a object URL, then open it in a new window.
how to add authentication header to $window.open
I think you can add this authentication parameters in URL and do a GET in your server side
//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);
window.open with headers
Can I control the HTTP headers sent by window.open (cross browser)?
No
If not, can I somehow window.open a page that then issues my request with custom headers inside its popped-up window?
- You can request a URL that triggers a server side program which makes the request with arbitrary headers and then returns the response
- You can run JavaScript (probably saying goodbye to Progressive Enhancement) that uses XHR to make the request with arbitrary headers (assuming the URL fits within the Same Origin Policy) and then process the result in JS.
I need some cunning hacks...
It might help if you described the problem instead of asking if possible solutions would work.
Related Topics
Convert an Image into Binary Data in JavaScript
Ajax Success Event Not Working
How to Use Blob Url, Mediasource or Other Methods to Play Concatenated Blobs of Media Fragments
Es6 Promises - Something Like Async.Each
How to Test If a Letter in a String Is Uppercase or Lowercase Using JavaScript
ASP.NET MVC 3 Razor: Include JavaScript File in the Head Tag
Jquery-Ui-Dialog - How to Hook into Dialog Close Event
Remove Characters from a String
Wrapping a Set of Dom Elements Using JavaScript
How to Serialize an Input File Object to JSON
Does JavaScript Process Using an Elastic Racetrack Algorithm
Call JavaScript Function from Url/Address Bar
Switch Case Not Showing Correct Results
Pass JavaScript Variable to Flask Url_For
How to Accomplish an If/Else in Mustache.Js
Calling Method Using JavaScript Prototype
Can't Access Cookies from Document.Cookie in Js, But Browser Shows Cookies Exist