如何通过javascript ajax获取pdf文件 [英] How to get pdf file via javascript ajax
本文介绍了如何通过javascript ajax获取pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想打开由微服务(字节[])生成的js文件:
I'd like to open my js file genereted by a microservice (byte[]):
public int getLabel(Request request, Response response) throws IOException {
response.header("Access-Control-Allow-Origin", "*");
ArrayList<JSONObject> orders = createListOfJSONObjects(request.queryParams("orders"));
response.header("Content-Type", "application/pdf");
ServletOutputStream outputStream = response.raw().getOutputStream();
outputStream.write(createPDF(orders));
outputStream.flush();
outputStream.close();
return 200;
}
我想收到ajax的回复:
I'd like to have the response by ajax:
$("#printlabels").click(function() {
var jsonData = $(this).data().ordersJson;
console.log(jsonData)
$.ajax({
// type: GET,
async: true,
url: "http://localhost:60000/api/create-label",
data: {orders: jsonData},
success: function(resp){
???;
}
});
});
我希望我的浏览器打开或保存pdf文件
I want my browser to open or save the pdf file
推荐答案
不要使用Ajax,请尝试以下操作:
Don't use ajax, try this:
$("#printlabels").click(function() {
var jsonData = $(this).data().ordersJson;
var url = "http://localhost:60000/api/create-label";
var $form = $('<form method="get" action="' + url + '" target="_blank" />');
for ( var key in jsonData ) {
$form.append('<input type="hidden" name="' + key + '" value="' + jsonData[key] + '" /> ');
}
$form.submit();
});
请阅读以下为何无法使用Ajax进行操作的说明: https://stackoverflow.com/a/9821924/5611328
Read this why you cannot do this with ajax: https://stackoverflow.com/a/9821924/5611328
这篇关于如何通过javascript ajax获取pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文