title: Casperjs ajax calls - waitForResource and parse errors
body: <p>Casperjs is a great tool. I have been able to understand certain basics. However, I am trying to tackle two issues. First, I split my code into several functions for closure reasons. I am getting a parse error. I have added the option to <code>verbose: true,  logLevel: "debug"</code> and see if it points out a line number but I get no results. Second, I want to make an ajax call, which are async. I am using <code>waitFor</code> but I read <a href="https://ghostinspector.com/blog/making-casperjs-tests-more-reliable/." rel="nofollow">THIS</a> article for a more efficient way. How can I set <code>waitForResource()</code> to track AJAX requests? and how to display parse error lines?</p>
  
  <p>CODE</p>
  
  <pre><code>var urls = ['http://9gag.tv/'];
  var casper = require('casper').create({
      verbose: true,
      logLevel: "debug"
  });
  
  function getNumberOfItems(casper) {
      return casper.getElementsInfo(".listview .badge-grid-item").length;
  }
  
  function tryAndScroll(casper) {
    casper.page.scrollPosition = { top: casper.page.scrollPosition["top"] + 4000, left: 0 };
    var info = casper.getElementInfo('.badge-post-grid-load-more');
    if (info.visible) {
      var curItems = getNumberOfItems(casper);
      if( curItems &lt;= 60 ) {
          casper.waitFor(function check(){
            return curItems != getNumberOfItems(casper);
          }, function then(){
            tryAndScroll(this);
          }, function onTimeout(){
            this.echo("Timout reached");
          }, 10000);
      }
    } else {
      casper.echo("no more items");
    }
  
  }
  
  function combineArrays(x, y) {
      var result = [];
      for(var i = 0, i &lt; page_links.length; i++) {
          result[i] = {};
          result[i].page_link = x[i];
          result[i].video_link = y[i];
      }
      return result;
  }
  
  function linkScraper(x){
      var page_links = [];
      var youtube = [];
      for (var i = 0; i &lt; x.length; i++)
      { // start for loop
          casper.thenOpen(x[i], function() {
              //Scroll down for elements
              tryAndScroll(this);
              casper.then(function() {
                this.getElementsInfo('.title').forEach(function(element) {
                  // skip elements that don't have a href attribute...
                  if (!element.attributes.href) {
                    return;
                  }
                  page_links.push( element["attributes"]["href"] );
                  casper.thenOpen(element.attributes.href, function() {
                    this.click('.responsivewrapper');
                  }).then(function(){
                    casper.each(this.getElementsInfo('.badge-youtube-player'), function(casper, element, j) {
                      youtube.push( element["attributes"]["src"] );
                    });
                  })localhost;
                });
              });
          });
      }
      return combineArrays(page_links,youtube);
  }
  
  function stringifyResult(webpages){
      //Pass link array to linkScraper
      var linksArr = linkScraper.call(this, webpages);
      //send results to php page
       server = "http://localhost:8181/hashtag_pull/lib/9GagPrivateApi.php";
       this.waitFor( function() {
           response = this.evaluate(function() {
              $.ajax({
                  type: "POST",
                  url: server,
                  data: JSON.stringify(linksArr),
                  //dataType: 'json',
                  contentType: "application/json",
                  success: function (data) {
                      this.echo("All done.");
                      return this.exit();
                      //return data.responseText;
                  },
                  error: function (xhr,status,error){
                      return this.echo(error);
                  }
              });
           });
       });
  }
  
  
  
  casper.start().then(function() {
      this.echo("Starting");
  });
  casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');
  casper.run(stringifyResult.call(this, urls));
  </code></pre>