[英]how to get value from ajax response in different textfields if there is more than one value in response?
[英]How to get more than 1 response in ajax called
我正在從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響應?
在您的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);
為什么不將響應包裝到json樣式字符串中,以便可以在json對象中處理ajax調用結果。 在您的情況下,您的jsp可以像這樣響應結果“ {{fileName':'test.xlsx','jsonString':'****','chintan':'***'}”
如果有多個,還可以將其標記為json對象中的數組
ajax中的單個請求將僅獲得單個響應。 您可以創建與此相關的提供了更多的要求,優秀的解決方案在這里 ,
要么
更好的方法是將額外的參數附加到json字符串本身中,並更改JS中的邏輯以在解析json字符串的同時從excel數據中提取這兩個值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.