Search code examples
javascriptcross-domaincasperjs

Download a file cross-domain in CasperJS


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


Solution

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