Reputation: 664
I am currently trying to fetch json values(mainly urls) from a GET call and assign this to a variable. I would ultimlately like to loop through the values and open each url with casper. However, I seen to have the incorrect concept on fetching values through an ajax call with casperjs. I read through the documentation but dont seem to understand why I am still getting error ReferenceError: Can't find variable: __utils__
?
casper.start();
var url = "http://dev.web-ui.com/generate.php";
casper.then(function(url) {
var results = __utils__.sendAJAX(url, "GET");
});
casper.run();
Upvotes: 0
Views: 711
Reputation: 61892
You have at least two problem:
The url
parameter is not a URL, but the last loaded page resource object which contains the URL.
__utils__
is not available outside of the page context. You can require it if you want, but that probably won't fix your problem, because the dummy document.location
outside of the page context has not the same domain as the URL you want to query, so that request may fail due to cross-domain restrictions. It's best to do this in the page context.
Example code:
casper.then(function(resource) {
var results = this.evaluate(function(url){
return __utils__.sendAJAX(url, "GET");
}, resource.url);
this.echo(results);
});
Upvotes: 1
Reputation: 763
Are you inside a casper test ? If so, maybe var __utils__ = require('clientutils').create();
would fix it. I can't try it myself right now unfortunately.
Upvotes: 0