JQuery ajax form submit for return file giving undefined error response [duplicate]
This question already has an answer here:
Download pdf file using jquery ajax
2 answers
Due to various issues with security of file manipulation, I am forced to submit the request via Ajax post method call. The server side URL receives this and gives out a pdf file as response. Server side code in java servlet is as below.
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=" + outFile);
//create the pdf file
response.flushBuffer();
On the client side, the javascript code is sending call to a POST which calls the above code via doPost. When the form is submitted through form.submit(), the file gets downloaded nicely as pdf file. When trying through Ajax call, it is giving me undefined response under error section. Client side code as below.
var formData = new FormData();
//Fill formData with fields and files
console.log('Posting form from ajax');
$.ajax({
url: "/createPdf",
type: "POST",
data: formData,
contentType: false,
processData: false,
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
console.log('Processing response');
download(response, "merged.pdf", "application/pdf" );
},
error: function (xhr, status, error)
{
var err = xhr.responseText;
alert(err);
}
In the above, it is not going under success response. It is going under error and alerting as undefined. What am I doing wrong here. My jquery version is 1.8.0.min.js if that matters. download function refers to http://danml.com/download.html but I have tried the other snippets too but it doesn't even come to success.
Under Inspection tools, I see success request and blob data in response, but it takes about 10-15 seconds for the blob data to show under Network in browser
Please help. I have been at it all evening and can't seem to figure out undefined response.
javascript jquery ajax
marked as duplicate by Phil
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 5:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 4 more comments
This question already has an answer here:
Download pdf file using jquery ajax
2 answers
Due to various issues with security of file manipulation, I am forced to submit the request via Ajax post method call. The server side URL receives this and gives out a pdf file as response. Server side code in java servlet is as below.
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=" + outFile);
//create the pdf file
response.flushBuffer();
On the client side, the javascript code is sending call to a POST which calls the above code via doPost. When the form is submitted through form.submit(), the file gets downloaded nicely as pdf file. When trying through Ajax call, it is giving me undefined response under error section. Client side code as below.
var formData = new FormData();
//Fill formData with fields and files
console.log('Posting form from ajax');
$.ajax({
url: "/createPdf",
type: "POST",
data: formData,
contentType: false,
processData: false,
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
console.log('Processing response');
download(response, "merged.pdf", "application/pdf" );
},
error: function (xhr, status, error)
{
var err = xhr.responseText;
alert(err);
}
In the above, it is not going under success response. It is going under error and alerting as undefined. What am I doing wrong here. My jquery version is 1.8.0.min.js if that matters. download function refers to http://danml.com/download.html but I have tried the other snippets too but it doesn't even come to success.
Under Inspection tools, I see success request and blob data in response, but it takes about 10-15 seconds for the blob data to show under Network in browser
Please help. I have been at it all evening and can't seem to figure out undefined response.
javascript jquery ajax
marked as duplicate by Phil
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 5:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Why don't you try logging the other two arguments passed to yourerror
callback (ieconsole.error(status, error)
)? Who knows, they could be useful.
– Phil
Nov 21 '18 at 5:00
try to find if there is problem in serverside if any so put System.out.print to find out the server side logging.
– Rahul Dudharejiya
Nov 21 '18 at 5:03
@RahulDudharejiya OP says "Under Inspection tools, I see success request and blob data in response" so presumably that isn't a problem
– Phil
Nov 21 '18 at 5:04
if your response is completely perfect then it should logging into success so there must be some problem in server side, try to debug at server side
– Rahul Dudharejiya
Nov 21 '18 at 5:06
@RahulDudharejiya, like Phil mentioned, if I try through normal form.submit() it gets downloaded from same server side without any issues, so not an issue there.
– Karthik
Nov 21 '18 at 5:08
|
show 4 more comments
This question already has an answer here:
Download pdf file using jquery ajax
2 answers
Due to various issues with security of file manipulation, I am forced to submit the request via Ajax post method call. The server side URL receives this and gives out a pdf file as response. Server side code in java servlet is as below.
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=" + outFile);
//create the pdf file
response.flushBuffer();
On the client side, the javascript code is sending call to a POST which calls the above code via doPost. When the form is submitted through form.submit(), the file gets downloaded nicely as pdf file. When trying through Ajax call, it is giving me undefined response under error section. Client side code as below.
var formData = new FormData();
//Fill formData with fields and files
console.log('Posting form from ajax');
$.ajax({
url: "/createPdf",
type: "POST",
data: formData,
contentType: false,
processData: false,
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
console.log('Processing response');
download(response, "merged.pdf", "application/pdf" );
},
error: function (xhr, status, error)
{
var err = xhr.responseText;
alert(err);
}
In the above, it is not going under success response. It is going under error and alerting as undefined. What am I doing wrong here. My jquery version is 1.8.0.min.js if that matters. download function refers to http://danml.com/download.html but I have tried the other snippets too but it doesn't even come to success.
Under Inspection tools, I see success request and blob data in response, but it takes about 10-15 seconds for the blob data to show under Network in browser
Please help. I have been at it all evening and can't seem to figure out undefined response.
javascript jquery ajax
This question already has an answer here:
Download pdf file using jquery ajax
2 answers
Due to various issues with security of file manipulation, I am forced to submit the request via Ajax post method call. The server side URL receives this and gives out a pdf file as response. Server side code in java servlet is as below.
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=" + outFile);
//create the pdf file
response.flushBuffer();
On the client side, the javascript code is sending call to a POST which calls the above code via doPost. When the form is submitted through form.submit(), the file gets downloaded nicely as pdf file. When trying through Ajax call, it is giving me undefined response under error section. Client side code as below.
var formData = new FormData();
//Fill formData with fields and files
console.log('Posting form from ajax');
$.ajax({
url: "/createPdf",
type: "POST",
data: formData,
contentType: false,
processData: false,
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
console.log('Processing response');
download(response, "merged.pdf", "application/pdf" );
},
error: function (xhr, status, error)
{
var err = xhr.responseText;
alert(err);
}
In the above, it is not going under success response. It is going under error and alerting as undefined. What am I doing wrong here. My jquery version is 1.8.0.min.js if that matters. download function refers to http://danml.com/download.html but I have tried the other snippets too but it doesn't even come to success.
Under Inspection tools, I see success request and blob data in response, but it takes about 10-15 seconds for the blob data to show under Network in browser
Please help. I have been at it all evening and can't seem to figure out undefined response.
This question already has an answer here:
Download pdf file using jquery ajax
2 answers
javascript jquery ajax
javascript jquery ajax
asked Nov 21 '18 at 4:52
KarthikKarthik
170312
170312
marked as duplicate by Phil
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 5:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Phil
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 5:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Why don't you try logging the other two arguments passed to yourerror
callback (ieconsole.error(status, error)
)? Who knows, they could be useful.
– Phil
Nov 21 '18 at 5:00
try to find if there is problem in serverside if any so put System.out.print to find out the server side logging.
– Rahul Dudharejiya
Nov 21 '18 at 5:03
@RahulDudharejiya OP says "Under Inspection tools, I see success request and blob data in response" so presumably that isn't a problem
– Phil
Nov 21 '18 at 5:04
if your response is completely perfect then it should logging into success so there must be some problem in server side, try to debug at server side
– Rahul Dudharejiya
Nov 21 '18 at 5:06
@RahulDudharejiya, like Phil mentioned, if I try through normal form.submit() it gets downloaded from same server side without any issues, so not an issue there.
– Karthik
Nov 21 '18 at 5:08
|
show 4 more comments
Why don't you try logging the other two arguments passed to yourerror
callback (ieconsole.error(status, error)
)? Who knows, they could be useful.
– Phil
Nov 21 '18 at 5:00
try to find if there is problem in serverside if any so put System.out.print to find out the server side logging.
– Rahul Dudharejiya
Nov 21 '18 at 5:03
@RahulDudharejiya OP says "Under Inspection tools, I see success request and blob data in response" so presumably that isn't a problem
– Phil
Nov 21 '18 at 5:04
if your response is completely perfect then it should logging into success so there must be some problem in server side, try to debug at server side
– Rahul Dudharejiya
Nov 21 '18 at 5:06
@RahulDudharejiya, like Phil mentioned, if I try through normal form.submit() it gets downloaded from same server side without any issues, so not an issue there.
– Karthik
Nov 21 '18 at 5:08
Why don't you try logging the other two arguments passed to your
error
callback (ie console.error(status, error)
)? Who knows, they could be useful.– Phil
Nov 21 '18 at 5:00
Why don't you try logging the other two arguments passed to your
error
callback (ie console.error(status, error)
)? Who knows, they could be useful.– Phil
Nov 21 '18 at 5:00
try to find if there is problem in serverside if any so put System.out.print to find out the server side logging.
– Rahul Dudharejiya
Nov 21 '18 at 5:03
try to find if there is problem in serverside if any so put System.out.print to find out the server side logging.
– Rahul Dudharejiya
Nov 21 '18 at 5:03
@RahulDudharejiya OP says "Under Inspection tools, I see success request and blob data in response" so presumably that isn't a problem
– Phil
Nov 21 '18 at 5:04
@RahulDudharejiya OP says "Under Inspection tools, I see success request and blob data in response" so presumably that isn't a problem
– Phil
Nov 21 '18 at 5:04
if your response is completely perfect then it should logging into success so there must be some problem in server side, try to debug at server side
– Rahul Dudharejiya
Nov 21 '18 at 5:06
if your response is completely perfect then it should logging into success so there must be some problem in server side, try to debug at server side
– Rahul Dudharejiya
Nov 21 '18 at 5:06
@RahulDudharejiya, like Phil mentioned, if I try through normal form.submit() it gets downloaded from same server side without any issues, so not an issue there.
– Karthik
Nov 21 '18 at 5:08
@RahulDudharejiya, like Phil mentioned, if I try through normal form.submit() it gets downloaded from same server side without any issues, so not an issue there.
– Karthik
Nov 21 '18 at 5:08
|
show 4 more comments
2 Answers
2
active
oldest
votes
For ajax download you should dynamic add link, trigger click and then delete.
var formData = new FormData();
//Fill formData with fields and files
console.log('Posting form from ajax');
$.ajax({
url: "/createPdf",
type: "POST",
data: formData,
contentType: false,
processData: false,
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(response);
a.href = url;
a.download = 'myfile.pdf';
a.click();
window.URL.revokeObjectURL(url);
},
error: function (xhr, status, error)
{
var err = xhr.responseText;
alert(err);
}
I had this normal flow instead of download.js too, but the issue is not dynamic link and clicking, it does not even enter the success block after Ajax response. It goes to error block and the alert shows as undefined.
– Karthik
Nov 21 '18 at 17:49
add a comment |
You mentioned in your form.submit () it works without any issue. What type are you using, POST or GET? I World try to change it in your ajax request. I'm no java pro but maybe your Servlet only reacts to GET requests. When I worked with Ajax and Servlets I used http Servlet where I overwrote the doGet method.
I have used POST method and Servlet is implementing the doPost method. form.submit() is using post as form is defined with method="post"
– Karthik
Nov 21 '18 at 17:52
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For ajax download you should dynamic add link, trigger click and then delete.
var formData = new FormData();
//Fill formData with fields and files
console.log('Posting form from ajax');
$.ajax({
url: "/createPdf",
type: "POST",
data: formData,
contentType: false,
processData: false,
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(response);
a.href = url;
a.download = 'myfile.pdf';
a.click();
window.URL.revokeObjectURL(url);
},
error: function (xhr, status, error)
{
var err = xhr.responseText;
alert(err);
}
I had this normal flow instead of download.js too, but the issue is not dynamic link and clicking, it does not even enter the success block after Ajax response. It goes to error block and the alert shows as undefined.
– Karthik
Nov 21 '18 at 17:49
add a comment |
For ajax download you should dynamic add link, trigger click and then delete.
var formData = new FormData();
//Fill formData with fields and files
console.log('Posting form from ajax');
$.ajax({
url: "/createPdf",
type: "POST",
data: formData,
contentType: false,
processData: false,
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(response);
a.href = url;
a.download = 'myfile.pdf';
a.click();
window.URL.revokeObjectURL(url);
},
error: function (xhr, status, error)
{
var err = xhr.responseText;
alert(err);
}
I had this normal flow instead of download.js too, but the issue is not dynamic link and clicking, it does not even enter the success block after Ajax response. It goes to error block and the alert shows as undefined.
– Karthik
Nov 21 '18 at 17:49
add a comment |
For ajax download you should dynamic add link, trigger click and then delete.
var formData = new FormData();
//Fill formData with fields and files
console.log('Posting form from ajax');
$.ajax({
url: "/createPdf",
type: "POST",
data: formData,
contentType: false,
processData: false,
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(response);
a.href = url;
a.download = 'myfile.pdf';
a.click();
window.URL.revokeObjectURL(url);
},
error: function (xhr, status, error)
{
var err = xhr.responseText;
alert(err);
}
For ajax download you should dynamic add link, trigger click and then delete.
var formData = new FormData();
//Fill formData with fields and files
console.log('Posting form from ajax');
$.ajax({
url: "/createPdf",
type: "POST",
data: formData,
contentType: false,
processData: false,
xhrFields: {
responseType: 'blob'
},
success: function (response, status, xhr) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(response);
a.href = url;
a.download = 'myfile.pdf';
a.click();
window.URL.revokeObjectURL(url);
},
error: function (xhr, status, error)
{
var err = xhr.responseText;
alert(err);
}
answered Nov 21 '18 at 16:04
dganencodganenco
2217
2217
I had this normal flow instead of download.js too, but the issue is not dynamic link and clicking, it does not even enter the success block after Ajax response. It goes to error block and the alert shows as undefined.
– Karthik
Nov 21 '18 at 17:49
add a comment |
I had this normal flow instead of download.js too, but the issue is not dynamic link and clicking, it does not even enter the success block after Ajax response. It goes to error block and the alert shows as undefined.
– Karthik
Nov 21 '18 at 17:49
I had this normal flow instead of download.js too, but the issue is not dynamic link and clicking, it does not even enter the success block after Ajax response. It goes to error block and the alert shows as undefined.
– Karthik
Nov 21 '18 at 17:49
I had this normal flow instead of download.js too, but the issue is not dynamic link and clicking, it does not even enter the success block after Ajax response. It goes to error block and the alert shows as undefined.
– Karthik
Nov 21 '18 at 17:49
add a comment |
You mentioned in your form.submit () it works without any issue. What type are you using, POST or GET? I World try to change it in your ajax request. I'm no java pro but maybe your Servlet only reacts to GET requests. When I worked with Ajax and Servlets I used http Servlet where I overwrote the doGet method.
I have used POST method and Servlet is implementing the doPost method. form.submit() is using post as form is defined with method="post"
– Karthik
Nov 21 '18 at 17:52
add a comment |
You mentioned in your form.submit () it works without any issue. What type are you using, POST or GET? I World try to change it in your ajax request. I'm no java pro but maybe your Servlet only reacts to GET requests. When I worked with Ajax and Servlets I used http Servlet where I overwrote the doGet method.
I have used POST method and Servlet is implementing the doPost method. form.submit() is using post as form is defined with method="post"
– Karthik
Nov 21 '18 at 17:52
add a comment |
You mentioned in your form.submit () it works without any issue. What type are you using, POST or GET? I World try to change it in your ajax request. I'm no java pro but maybe your Servlet only reacts to GET requests. When I worked with Ajax and Servlets I used http Servlet where I overwrote the doGet method.
You mentioned in your form.submit () it works without any issue. What type are you using, POST or GET? I World try to change it in your ajax request. I'm no java pro but maybe your Servlet only reacts to GET requests. When I worked with Ajax and Servlets I used http Servlet where I overwrote the doGet method.
answered Nov 21 '18 at 5:52
scpscp
22
22
I have used POST method and Servlet is implementing the doPost method. form.submit() is using post as form is defined with method="post"
– Karthik
Nov 21 '18 at 17:52
add a comment |
I have used POST method and Servlet is implementing the doPost method. form.submit() is using post as form is defined with method="post"
– Karthik
Nov 21 '18 at 17:52
I have used POST method and Servlet is implementing the doPost method. form.submit() is using post as form is defined with method="post"
– Karthik
Nov 21 '18 at 17:52
I have used POST method and Servlet is implementing the doPost method. form.submit() is using post as form is defined with method="post"
– Karthik
Nov 21 '18 at 17:52
add a comment |
Why don't you try logging the other two arguments passed to your
error
callback (ieconsole.error(status, error)
)? Who knows, they could be useful.– Phil
Nov 21 '18 at 5:00
try to find if there is problem in serverside if any so put System.out.print to find out the server side logging.
– Rahul Dudharejiya
Nov 21 '18 at 5:03
@RahulDudharejiya OP says "Under Inspection tools, I see success request and blob data in response" so presumably that isn't a problem
– Phil
Nov 21 '18 at 5:04
if your response is completely perfect then it should logging into success so there must be some problem in server side, try to debug at server side
– Rahul Dudharejiya
Nov 21 '18 at 5:06
@RahulDudharejiya, like Phil mentioned, if I try through normal form.submit() it gets downloaded from same server side without any issues, so not an issue there.
– Karthik
Nov 21 '18 at 5:08