我正在从 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 调用时,Handel 3 如何在 JSP 中响应......?
最佳答案
引用链接 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);
关于java - 如何在ajax调用中获得超过1个响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26376640/