0

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();
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
MaryCoding
  • 624
  • 1
  • 9
  • 31

2 Answers2

1

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);
});
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
0

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.

Willy
  • 763
  • 2
  • 8
  • 29
  • Oh I did miss that. Now added that part but now I am getting `NETWORK_ERR: XMLHttpRequest Exception 101: A network error occured in synchronous requests.` – MaryCoding Oct 15 '15 at 04:12
  • Just by googling a bit : https://stackoverflow.com/questions/15913170/phantom-js-synchronous-ajax-request-network-err-xmlhttprequest-exception-101 – Willy Oct 15 '15 at 04:47
  • yes, i have it set to false for web security - `var casper = require('casper').create({ pageSettings: { webSecurityEnabled: false } });` – MaryCoding Oct 15 '15 at 04:50
  • In the docs they say to pass it in the CLI call `Don’t forget to pass the --web-security=no option in your CLI call in order to perform cross-domains requests when needed` Since you seem to do a cross domain request that's probably the cause. – Willy Oct 15 '15 at 04:52