Issue
I am uploading a file from JSP and processing it in a servlet and returning data to JSP.
JSP file that uploads the file:
$(document).ready(function() {
$(':file').change(function(){
var fileObj = this.files[0];
var form = $('#upload');
var fd = new FormData();
fd.append( 'file', fileObj);
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: fd,
processData: false,
contentType: false,
async: false,
}).done(function(data){
alert('ajax complete');
$('#previewForm').append("<div>" + data + "</div>");
$('#ldiv').hide();
}).fail(function() {
alert("error");
$('#ldiv').hide();
});
}
Servlet file.
That read file which is upload by the jsp suppose upload Excel file. read that file data and convert that data into jsonString. Now I want to send that string as response of ajax call
I need to return more then 1 value
System.out.print("test.xlsx"); //File name
System.out.print(jsonSting); // jsonSting is variable that is data of excel file which convert in json
System.out.print("chintan"); //other parameter.
So how can i Handel 3 response in JSP while ajax called....?
Solution
Refer Link here In your jsp page
import org.json.simple.JSONObject;
JSONObject obj = new JSONObject();
obj.put("fileName", "test.xlsx");
obj.put("jsonSting", jsonSting);
obj.put("name", "chintan");
In Your ajax response
var json = $.parseJSON(data);
$('#results').html('Filename name: ' + json.fileName + '<br />jsonSting: ' + json.jsonSting);
Answered By - Yatin Mistry