Skip to content

Instantly share code, notes, and snippets.

@markstreich
Created September 7, 2012 17:27
Show Gist options
  • Save markstreich/3667944 to your computer and use it in GitHub Desktop.
Save markstreich/3667944 to your computer and use it in GitHub Desktop.
PhantomJS: Ajax response capture through console.log
var page = require('webpage').create();
page.open('http://www.website.com/', function (status) {
if (status === 'success') {
captureAjaxResponsesToConsole();
}
});
function captureAjaxResponsesToConsole() {
// logs ajax response contents to console so sublime's onConsoleMessage can use the contents
// credit to Ionuț G. Stan
// http://stackoverflow.com/questions/629671/how-can-i-intercept-xmlhttprequests-from-a-greasemonkey-script
page.evaluate(function() {
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
this.addEventListener("readystatechange", function() {
if (this.readyState === 4) {
var res={'response':this.responseText, 'url':url};
console.log(JSON.stringify(res));
}
}, false);
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
return 1;
});
}
page.onConsoleMessage = function (msg) {
var res=JSON.parse(msg);
console.log('--------------------');
console.log('URL:' + res.url);
console.log('Response: ' + res.response);
};
@osamamamdouh
Copy link

I failed to get success response can you give me example with real variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment