简体   繁体   中英

Javascript - How would I send a http request without it being redirected?

I am trying to send a http request to another site on a different domain. If you don't think that this is possible, send a redirected link to this site, and it will come up with the "Location" header. All I want is the location header, since this website redirects me to a different key every time you visit it.

How would I do exactly what this site is doing, but only for the "Location" header? All I want is to access the location for which the site is sending me to.

var request = new Request('https://example.com/redirectThatDownloadsThenCloses', {
method: 'GET', 
mode: 'no-cors', 
redirect: 'manual',
headers: new Headers()
});
fetch(request).then(response => {        
const headers = response.headers.entries();
   let header = headers.next();
     console.log(header.value);
});

In javascript you can do an XmlHttpRequest to a different domain only if the domain you are calling respond with an Access-Control-Allow-Origin.
To retrieve only header you need a call with verb HEAD.
If the other domain doesn't allow cross domain request I suggest to do the request server side where there are no limitation.
If you only need to call the url without read the response you can load the url into an hidden iframe.

<iframe id="myIframe" src="#" style`="display:none"></`iframe>

document.getElementById("myIframe").src=myUrl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM