I am using JavaScript that makes a call to a server (Java) to get documents. It has an Ajax call:
Client (JavaScript):
$.ajax({
type: "get",
url: urlStringCustomerInvoices,
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (result) {
if (result.length > 0) {
The server (Java) sets a byte array representing the PDF document (can also be other document types):
OutputStream os = response.getOutputStream();
bos = new BufferedOutputStream(os);
bos.write(bytes);
bos.flush();
os.flush();
os.close();
This works as expected, i.e. a document, e.g. PDF can be downloaded sucessfully. However, when there is a problem, I want to be able handle it. If the returned result
is not a valid document.
On the client in JavaScript, how do I validate if the returned result
is a valid document?
Valid documents are file types .pdf
, .doc
and .docx
The type seems to always be String
:
console.log(Object.prototype.toString.call(result));
prints:
[object String]
Also:
console.log(result.length);
prints (for a valid PDF):
66402