Using Firefox
I am trying to download some data from Google Drive
using XMLHttpRequest
. In the debug console it gives me [302 Moved Temporarily]
and the data i receive is empty. How can i get XMLHttpRequest
to follow a redirect response? Also I am using https if it changes things.
Using a XMLHttpRequest in async mode, the redirects are followed and it works as expected,; but in synchronous mode it fails.
To do this, open the Settings tab of your request and toggle off the Automatically follow redirects option. Force your request to follow the original HTTP method. To do so, open the Settings tab of the request and toggle on the Follow original HTTP method option.
Basiclly you get the Location using xhr.getResponseHeader("Location")
. In this case you could just send another XMLHttpRequest
to this location using the same parameter:
function ajax(url /* ,params */, callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
// return if not ready state 4
if (this.readyState !== 4) {
return;
}
// check for redirect
if (this.status === 302 /* or may any other redirect? */) {
var location = this.getResponseHeader("Location");
return ajax.call(this, location /*params*/, callback);
}
// return data
var data = JSON.parse(this.responseText);
callback(data);
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With