Browse Source

httpGet: fallback to load-via-server; option 'forceServerLoad'

master
parent
commit
c6fa5b1f39
  1. 2
      httpGet.php
  2. 36
      src/httpGet.js

2
httpGet.php

@ -0,0 +1,2 @@
<?php
readfile($_REQUEST['url']);

36
src/httpGet.js

@ -1,9 +1,14 @@
function httpGet (url, options, callback) { function httpGet (url, options, callback) {
var xhr = new XMLHttpRequest()
xhr.open('get', url, true)
xhr.responseType = 'text'
xhr.onreadystatechange = function () {
let corsRetry = true
var xhr
function readyStateChange () {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
if (corsRetry && xhr.status === 0) {
corsRetry = false
return viaServer()
}
if (xhr.status === 200) { if (xhr.status === 200) {
callback(null, { body: xhr.responseText }) callback(null, { body: xhr.responseText })
} else { } else {
@ -11,7 +16,28 @@ function httpGet (url, options, callback) {
} }
} }
} }
xhr.send()
function direct () {
xhr = new XMLHttpRequest()
xhr.open('get', url, true)
xhr.responseType = 'text'
xhr.onreadystatechange = readyStateChange
xhr.send()
}
function viaServer () {
xhr = new XMLHttpRequest()
xhr.open('get', 'httpGet.php?url=' + encodeURIComponent(url), true)
xhr.responseType = 'text'
xhr.onreadystatechange = readyStateChange
xhr.send()
}
if (options.forceServerLoad) {
viaServer()
} else {
direct()
}
} }
module.exports = httpGet module.exports = httpGet
Loading…
Cancel
Save