mirror of
https://github.com/d0zingcat/cors-anywhere.git
synced 2026-05-13 15:09:25 +00:00
Remove manual redirect handling
From now on, redirects will automatically be handled by the browser. Using the API by clients has become extremely easy. Included JavaScript / jQuery snippets in the documentation to demonstrate that it's easy to use the API.
This commit is contained in:
25
demo.html
25
demo.html
@@ -66,34 +66,13 @@ textarea {
|
||||
<script>
|
||||
var protocol = location.protocol === 'http:' ? 'http:' : 'https:';
|
||||
var cors_api_url = protocol + '//cors-anywhere.herokuapp.com/';
|
||||
var cors_api_redirect_cache = {};
|
||||
function doCORSRequest(options, redirectCount) {
|
||||
var x = new XMLHttpRequest();
|
||||
x.open(options.method, cors_api_url + options.url);
|
||||
|
||||
x.onload = function() {
|
||||
if (x.status === 333) {
|
||||
redirectCount = +redirectCount ? +redirectCount + 1 : 1;
|
||||
var redirectInfo = /^(\d+) (.*)$/.exec(x.statusText);
|
||||
if (redirectInfo && redirectCount <= 5) {
|
||||
var originalStatus = +redirectInfo[1];
|
||||
var url = redirectInfo[2];
|
||||
if (originalStatus === 307 || originalStatus === 308) {
|
||||
// Correctly deal with method-preserving redirects
|
||||
options.url = url;
|
||||
doCORSRequest(options, redirectCount);
|
||||
} else {
|
||||
// Otherwise just change to GET
|
||||
doCORSRequest({
|
||||
method: 'GET',
|
||||
url: url,
|
||||
success: options.success,
|
||||
error: options.error
|
||||
}, redirectCount);
|
||||
}
|
||||
} else {
|
||||
options.error(options, x.status, x.statusText);
|
||||
}
|
||||
} else if (x.status >= 200 && x.status < 300 || x.status === 304) {
|
||||
if (x.status >= 200 && x.status < 300 || x.status === 304) {
|
||||
options.success(options, x.status, x.statusText, x.responseText);
|
||||
} else {
|
||||
options.error(options, x.status, x.statusText);
|
||||
|
||||
Reference in New Issue
Block a user