我正在从JSP上传文件,并在Servlet中对其进行处理,然后将数据返回给JSP。
上传文件的JSP文件:
$(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文件。
由jsp上传的读取文件
假设上传Excel文件。
读取文件数据并将其转换为jsonString。
现在我想发送该字符串作为ajax调用的响应
我需要返回大于1的值
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.
那么,当ajax调用....时,我如何在JSP中处理Handel 3响应?
最佳答案
请参阅链接here
在您的jsp页面中
import org.json.simple.JSONObject;
JSONObject obj = new JSONObject();
obj.put("fileName", "test.xlsx");
obj.put("jsonSting", jsonSting);
obj.put("name", "chintan");
在您的ajax响应中
var json = $.parseJSON(data);
$('#results').html('Filename name: ' + json.fileName + '<br />jsonSting: ' + json.jsonSting);