Wednesday, June 15, 2016

Download a file cross-domain in CasperJS

Leave a Comment

I'm unable to download a file stream from a web server using CasperJS:

  • a form is posted to a url
  • url returns a file stream

So far I have validated that the correct form values are posted.

var casper = require('casper').create({     verbose: true,      logLevel: 'debug',     viewportSize: {width: 1440, height: 800},     pageSettings: {         userName: '****',         password: '****',         webSecurityEnabled: false     },     waitTimeout: 200000 });  casper.start("***");  casper.then(function() {     var exportForm = this.evaluate(function() {         return $("#export_pdf_form").serialize();     });      var exportAction = this.evaluate(function() {         return $("#export_pdf_form").attr('action');     });      var url, file;     url = '***' + exportAction; (eg. https://webserver/export)     file = "export.pdf";     casper.page.settings.webSecurityEnabled = false;     casper.download(url, fs.workingDirectory + '/' + file, "POST", exportForm); }); 

Casper error "Unfortunately casperjs cannot make cross domain ajax requests" followed by "XMLHttpRequest Exception 101". After searching it states that settings the web security variable to false should make this work...but it doesn't. Anything else I should look into?

casperjs - v1.1.1 phantomjs - v2.0.0

3 Answers

Answers 1

Turns out nothing is wrong with my code, simply updating PhantomJS from 2.0.0 to 2.1.1 has solved the issue.

Answers 2

Alternative answer: You could implement a proxy, via an API interface through your site. Caveat: Best done only with resources that you control, as it requires your site to be responsible for the content, and could compromise your certificate if you allowed malware or insecure content.

Answers 3

There is a lot of AJAX cross-domain and same-origin security policy material written out there, take a look. As far as i know, there is only two alternatives to the one John proposed (setting up a proxy on the server side):

  1. Using W3C CORS standard technique and HTTP headers.

    https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

  2. JSONP mechanism.

    https://en.wikipedia.org/wiki/JSONP

I really don´t know if it is the real problem you are experiencing, but i hope this is helpfull for you.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment