Answer the question
In order to leave comments, you need to log in
How to handle redirects with AJAX?
Hello.
Easy background: I am writing a Javascript plugin for Set-Top Box Teletec MAG-250 to work with fs.to (this is not advertising).
The script parses the page and parses links to movies. The links look like:
_fs.to/get/play/1kqvftg8t6tgu.0.4278037390.974127405.1414671289_hd.mp4
The problem is that when this URL is requested, 2 more redirects occur. The set-top box player itself cannot cope with redirects, apparently getting "302 Moved Temporarily" it resets the connection. The path itself, for example, is as follows:
1. _fs.to/get/play/1kqvftg8t6tgu.0.4278037390.974127405.1414671289_hd.mp4 (302 Moved Temporarily)
2._filecdn.to/fsuaget/YzU0YmFlMWM4ODFmMDExNDJjNWMwNWUzZTZmZGQ2NThmc3RvfDMxMDQyMjU3MDh8MTAwMDB8MnwwfHw5YzA5ZTBkM2RlNjY4NTU5OTA5Y2VlYjNmZDcyZGI0NXwwfDE0MTQ2NzEzMDE,/play_1kqvftg8t6tgu.0.4278037390.974127405.1414671289.mp4 (302 Temporarily Moved)
3. _n25.filecdn.to/ff/YzU0YmFlMWM4ODFmMDExNDJjNWMwNWUzZTZmZGQ2NTh8ZnN0b3wzMTA0MjI1NzA4fDEwMDAwfDJ8MHw4fDI1fDAxNTkzOWE4NTE0YTg2ZjhiYTVjZDFhZTY2Njc0MDI1fDB8MjY6MS4xODpoLjQxOjEuMjM6ZnwwfDU2NjA0MjM2NnwxNDE0NjcxMzAyLjE4MDk,/play_1kqvftg8t6tgu.0.4278037390.974127405.1414671289.mp4 (200 OK)
I need to get the last link (No. 3).
Each GET request has a Location URL in its headers that will be redirected to.
The problem is that I can't pick up Header('Location') from the jqXHR object during redirects and I can't process the StatusCode (302 or 200). The request does not fall under the error method, AJAX goes through all redirects and starts loading the file somewhere in the cache.
$.ajax({
type: 'GET',
url: link1,
dataType: 'jsonp',
crossDomain: true,
statusCode: {
302: function(jqXHR) { // сюда мы не попадаем
var location = jqXHR.getResponseHeader('Location');
console.log(location);
}
},
error: function(jqXHR, textStatus, errorThrown) { // и сюда мы не попадаем
var location = jqXHR.getResponseHeader('Location');
console.log(location);
}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question