0

I am creating PDF file in server side and on returning this on servlet request. I am retrieving this using jquery ajax call, but is is giving me parse error, what could be reason?

server side code

response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("application/pdf");
String fileName = "Vendormaster.pdf";
response.setHeader("Content-Disposition","attachment; filename=\"" + fileName  + "\"");
RandomAccessFile f = new RandomAccessFile("D:/PDF_1.pdf", "r");
byte[] pdfBytes= new byte[(int)f.length()];
f.read(pdfBytes);
ServletOutputStream out = response.getOutputStream();
response.setContentLength(pdfBytes.length);
out.write(pdfBytes);
out.flush();

Ajax call

function GetData(url){  var dataObject=[];  $.ajax({

        url : url,
        type : "GET",
        async : false,
        dataType : 'json',
        contentType : "application/json",
        beforeSend:function(){ //           $("#ajaxloader").fadeIn();      },
        success : function(data, textStatus, jqXHR) {        dataObject= data; //        $("#ajaxloader").fadeOut(0);

        },
        error : function(data,b,c) {
            var error; //           $("#ajaxloader").fadeOut(0);
                return null;
        }

Header from Live Http headers

GET /vendor_master_new/PdfGen HTTP/1.1 Host: xx.xx.xxx.xx:50000 Accept: application/json, text/javascript, /; q=0.01 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Content-Type: application/json Cookie: saplb_*=(J2EE3952620)3952650; MYSAPSSO2=AjExMDAgABRwb3J0YWw6QWRtaW5pc3RyYXRvcogAB2RlZmF1bHQBAAACAAMwMDADAANFUDEEAAwyMDE0MDgyNjAyNDEFAAQAAAAICgAA%2FwEEMIIBAAYJKoZIhvcNAQcCoIHyMIHvAgEBMQswCQYFKw4DAhoFADALBgkqhkiG9w0BBwExgc8wgcwCAQEwIjAdMQwwCgYDVQQDEwNFUDExDTALBgNVBAsTBEoyRUUCAQAwCQYFKw4DAhoFAKBdMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTE0MDgyNjAyNDEyMFowIwYJKoZIhvcNAQkEMRYEFNI0asgM56O%2FhRIw9maLcMVSFKjLMAkGByqGSM44BAMELjAsAhRVFdOnzxSD1X6!YYuN3rFTWD4QGwIUa6cOuiyvPzHS355ksX7!%2Ft5az3c%3D; JSESSIONID=olcptllrixgcMgn5_xL0y4mRZzAQSAEKUDwA_SAP-1UuLDj55yIZYsgUl10Rvcmd; JSESSIONMARKID=ccttaAveOF7gPQfOE5gGjfKliPgTAVLdbcRwpQPAA Referer: http://xx.xx.xxx.xx:50000/vendor_master_new/vendorMaster.html User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36 X-Requested-With: XMLHttpRequest

HTTP/1.1 200 OK content-disposition: attachment; filename="Vendormaster.pdf" content-length: 480288 content-type: application/pdf date: Tue, 26 Aug 2014 03:10:54 GMT expires: Thu, 01 Jan 1970 00:00:00 GMT pragma: no-cache server: SAP NetWeaver Application Server 7.41 / AS Java 7.40

Arvind
  • 1,207
  • 6
  • 27
  • 55
  • 2
    because you return raw pdf file. It's not json format. – manassorn Aug 26 '14 at 03:48
  • No idea what you're trying to do with binary PDF data in your page, but the actual issue is similar to [this](http://stackoverflow.com/questions/25297982/display-image-in-html-which-is-returned-as-a-mime-media-type-from-an-ajax-call/25299278#25299278). – Robby Cornelissen Aug 26 '14 at 03:53
  • 1. Change your contentType in ajax call to what you set in server [application/pdf] 2. Refer this SO post, it is almost duplicate http://stackoverflow.com/questions/2186562/post-to-server-receive-pdf-deliver-to-user-w-jquery – spiderman Aug 26 '14 at 04:04

0 Answers0