MaryCoding
MaryCoding

Reputation: 664

Send an Ajax request in CasperJS and loop through results

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

Answers (2)

Artjom B.
Artjom B.

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

Willy
Willy

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

Related Questions