Reputation: 753
I am trying to make an ajax call with phantomjs.
The only resource i found regarding this is: http://phantomjs.org/api/webpage/method/evaluate-async.html
Although when i try to make an ajax call though evaluateasync, the code within is not being executed. Even console.log('Async'); not being printed.
Any ideas?
var webPage = require('webpage');
var page = webPage.create();
page.open("", function(status) {
page.includeJs("https://code.jquery.com/jquery-3.1.1.min.js", function() {
console.log('includejs');
page.evaluateAsync(function() {
console.log('Async');
$.ajax({
url : "some url",
type : 'post',
success : function(data) {
console.log(data);
console.log('success');
},
error : function(jqXHR, exception) {
console.log(jqXHR+'-'+exception);
console.log('error');
}
});
});
});});
Upvotes: 0
Views: 352
Reputation: 16838
page.evaluateAsync
is not ignored, it's just that console.log
inside of page.evaluate/evaluateAsync()
function is not the same as in the PhantomJS script, it's running in the sandboxed environment and uou have to subscribe to it:
page.onConsoleMessage = function(msg) {
console.log(msg);
};
Also, if you try to log an object, as in
console.log(jqXHR+'-'+exception);
you'll get [object Object] result, instead try
console.log(JSON.stringify(jqXHR)+'-'+exception);
Even more important: if after opening one site you're gonna do ajax requests to another, run your script with --web-security=false switch to allow cross-domain XHR.
phantomjs --web-security=false evalasync.js
Upvotes: 1