Last active
June 11, 2018 10:53
-
-
Save JamesMGreene/5340677 to your computer and use it in GitHub Desktop.
Example of (a) successfully executing a cross-domain XHR from the PhantomJS outer context to a wildcard CORS-enabled site; and (b) failing to execute a cross-domain XHR from the PhantomJS outer context to a site that is not CORS-enabled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var urls = [ | |
/* Wildcard CORS enabled - Works in PhantomJS 1.9.0 */ | |
'http://updates.html5rocks.com', | |
/* CORS disabled - Fails in PhantomJS 1.9.0 (and every other version) */ | |
'http://www.google.com', | |
/* Hack workaround? */ | |
/* | |
function(httpGet, callback) { | |
phantom.page.settings = (phantom.page.settings || {}); | |
phantom.page.settings.webSecurityEnabled = false; | |
phantom.page.setContent(phantom.page.content, phantom.page.url); | |
httpGet('http://www.google.com', callback); | |
console.log('waiting...'); | |
} | |
*/ | |
]; | |
phantom.onError = function(msg, trace) { | |
var msgStack = ['PHANTOM ERROR: ' + msg]; | |
if (trace) { | |
msgStack.push('TRACE:'); | |
trace.forEach(function(t) { | |
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : '')); | |
}); | |
} | |
console.error(msgStack.join('\n')); | |
phantom.exit(1); | |
}; | |
function httpGet(url, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.addEventListener('load', function() { | |
callback.call(xhr, null); | |
}); | |
xhr.addEventListener('error', function(err) { | |
callback.call(xhr, err || new Error('HTTP error!')); | |
}); | |
xhr.open('GET', url, false); | |
xhr.send(null); | |
} | |
var next = (function() { | |
var counter = 0; | |
var onCallback = function(err) { | |
console.log('GET ' + urls[counter]); | |
if (err) { | |
console.error('XHR Error!'); | |
console.error('status: ' + this.status + ' (' + this.statusText + ')'); | |
console.error('body:\n' + this.responseText + '\n'); | |
} | |
else { | |
console.log('XHR Loaded'); | |
console.log('status: ' + this.status + ' (' + this.statusText + ')'); | |
console.log('body size: ' + this.responseText.length + '\n'); | |
} | |
// Continue | |
counter++; | |
next(); | |
}; | |
// implementation: `next` | |
return function() { | |
var url = urls[counter]; | |
if (url) { | |
if (typeof url === 'string') { | |
httpGet(url, onCallback); | |
} | |
else if (typeof url === 'function') { | |
url(httpGet, onCallback); | |
} | |
else { | |
throw new TypeError('urls[' + counter + '] was of an unexpected type: ' + (typeof url)); | |
} | |
} | |
else { | |
phantom.exit(0); | |
} | |
} | |
})(); | |
next(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example not worked when I tried in 1.9.8 and 2.0.0.
Output in 1.9.8:
GET http://updates.html5rocks.com
XHR Error!
status: 0 ()
body:
GET http://www.google.com
XHR Error!
status: 0 ()
body:
Output in 2.0.0:
GET http://updates.html5rocks.com
XHR Loaded
status: 0 ()
body size: 0
GET http://www.google.com
XHR Loaded
status: 0 ()
body size: 0
Is XmlHttpRequest not worked in phantomjs?