我想打开由微服务(byte [])创建的js文件:
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的回复:
$("#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文件
答案 0 :(得分:0)
不要使用ajax,试试这个:
$("#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