This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Empty Response - Ajax

brettm247 wrote on 2015-12-13:
Hello,

I noticed the API was down for a bit today, i'm not sure if that is my issue or not. I am trying to connect to the API using Jquery and AJAX but am getting an empty response back

Here is my code: Note: {username} and {key} are replaced in my code.

$('#download').click(function () {
beforePrint();
var content = $("#ScaleForm")[0].innerHTML;
var request = $.ajax({
url: "http://pdfcrowd.com/api/pdf/convert/html/",
method: "POST",
data:{username:'{username}',key:'{key}',src:content},
contentType: "application/x-www-form-urlencoded",
headers: { 'Access-Control-Allow-Origin': '*'},
success: function(msg) {
console.log(msg.statusText);
},

error: function(msg) {
console.log(msg.statusText);
}
});
afterPrint();
});

Is this a CORS issue or am i missing something obvious?

Thanks!
support wrote on 2015-12-15:
Hello,

This is most likely a CORS issue as we do not implement it on our side.
brettm247 wrote on 2015-12-15:
Turned out to be a CORS issue.

I've since switched to the server handling the request.

Which leads me to... Can i pass a stylesheet using the "user_print_media" option similar to below? I am trying to include Form data users enter and when i just send the page, no filled in values are included. So i send the raw html.

app.post('/pdf', function (req, res) {
console.log('Connected to Pdf Route');
console.log(req.body.src);
var date = new Date();
var time = date.getUTCMilliseconds();
client.convertHtml(
req.body.src,
pdf.saveToFile("pdfs/" + req.business + time + ".pdf"),
{
width: "11in",
height: "8.5in",
vmargin: ".4in",
footer_html: '<div style=text-align:center;font-size:smaller;color:maroon;">\
Page %p out of %n\
</div>',
use_print_media:'example.com/css/mystylesheet.css'
});

res.download("pdfs/" + req.business + time + ".pdf","scaleForm.pdf");
res.send(200, { msg: "completed" });
});

Thanks!
brettm247 wrote on 2015-12-15:
nm, just append links to the style sheets in the html.
virtualLast wrote on 2017-04-03:
Can anyone explain how I can get around this CORS issue, I dont understand. I am using jquery, the post works when using postman.
@brettm247 - you said you switched servers? what does that mean