0

In CasperJS, I am working with a site that has a button like this:

<a id="showBills" 
   onclick="javascript:{showBills(); return false;}" 
   href="#">Show Bills</a>

Clicking it generates an XMLHttpRequest POST message with a massive form full of magic numbers and state. The server responds by sending a JSON data structure that I want to capture.

In this case, I can't simply call casper.getPageContent() to get the JSON data, since it's interpreted by the client and incorporated into the DOM.

Nor can I call casper.download(...) since I can't manually reconstruct the form data required for the POST request.

the question

How can I 'intercept' the JSON data sent in response to an onClick()-backed click?

half an idea

There may be some way to subvert the showBills() method on the client, for example to send the JSON response as an ordinary page (rather than an XMLHttpRequest). But that's just beyond my understanding of CasperJS, PhantomJS and HTTP protocols.

fearless_fool
  • 33,645
  • 23
  • 135
  • 217
  • You mean like the `casper.loadResource()` function from my answer [here](http://stackoverflow.com/a/24591244/1816580)? Or if it is purely textual data (JSON should be textual), then `__utils__.sendAJAX()` should do like it is described in my answer [here](http://stackoverflow.com/a/24561614/1816580). – Artjom B. Oct 02 '15 at 22:41
  • With apologies to @ArtjomB, I realize I'm asking the wrong question. I've rewritten it to make it useful. – fearless_fool Oct 04 '15 at 05:07
  • Have you tried the "Stateful AJAX Request" part from the [previously linked answer](http://stackoverflow.com/a/24561614/1816580)? I see no other sane way to get the response. Other avenues to explore would be (1) changing the implementation of `showBills` by `eval(showBills.toString().replace(...))` or (2) parsing the cached and gzipped resource from the cache directory if there even is one. – Artjom B. Oct 04 '15 at 08:58
  • If you're only interested in the JSON it might not make sense to use phantomjs/casperjs in the first place. Any regular request library will be better. – pguardiario Oct 05 '15 at 01:08

0 Answers0