pdf - CASPERJS ajax binary response inside evaluate() -


in project have download pdf server filling post form.

  • casper offers .download() - not working: have set several headers before send request.

  • phantom offers .customheaders() - not working: have no idea.

i have progress sending request ajax, emulating form fields.

        var =this.evaluate(function() {              try{                 var http = new xmlhttprequest();                    var ok = false;//return var                 var form = document.forms.nameditem('atlform');//get form                 var data = {};//params                 for(var = 0; < form.elements.length; i++) {//params populated                    data[form.elements[i].name] = encodeuricomponent(form.elements[i].value);                 }                 var url = form.action;//self expl.                  var dataf = object.keys(data).map(function(key) {                     return key + '=' + data[key];                 }).join('&');//param string format                 http.open("post", url, false);//open sync call                     http.setrequestheader("accept","text/html,application/xhtml+xml,application/xml;q=0.9,application/pdf,image/webp,image/apng,*/*;q=0.8");                   //[more headers]                    http.onreadystatechange = function() {                     if(http.readystate == 4 && http.status == 200) {                           ok=http.responsetext;                     }                     else{return false;}                 };                 http.send(dataf);                   return ok;              }catch(err){return err.message;}          });              console.log(a);          var fs = require('fs');         fs.write('pdf.pdf',a, 'w'); 

the code above correct. pdf file pdf headers, 140 lines on notepad (as expected), content blank. ajax response changed ascii, turning pdf trashy blank page, , further came.

setting http.responsetype = "arraybuffer" should solve problem, works in async calls.

the solution async ajax, console.log(a); line prints null, wait(), waitfor(), setinterval()//inside evaluate. can't response inside evaluate() , can/could/maybe put callback inside evaluate() i'm hopeless now. suggestions?


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -