[英]Access response data of ajax's success call back function
I returned a list<object>
from my controller,it successfully captured in ajax's success()
, as it is list so it can have n-number of objects, I want to create tabular data dynamically and populated the same by iterating data
, but I am not able to access the elements inside data
object, as console data shows, the actual elements are wrapped inside an outer object and my for loop outer one. 我从控制器返回了一个
list<object>
,它成功地捕获在ajax的success()
,因为它是list,所以它可以有n个对象,我想动态创建表格数据,并通过迭代data
填充表格data
,但是我无法访问data
对象内部的元素,如控制台数据所示,实际元素包装在一个外部对象内部,而我的for循环外部。 please see the screenshot attached 请参阅所附的屏幕截图
Please refer to this link for image reference: Console log 请参考此链接以获取图像参考: 控制台日志
Ajax call of the controller: 控制器的Ajax调用:
function getSelectedTableRecords(tableId) {
if (tableId != null && tableId != '') {
$.ajax({
type: "POST",
url: baseUrl + "search",
data: {
tableId: tableId
},
success: function (data) {
for (var i = 0; i < data.length; i++) {
var item = data[i];
$('#applicationList > tbody').append(
'<tr>'
+ '<td><h4>' + item.userId + '</h4></td>'
+ '<td><h4>' + item.firstName + '</h4></td>'
+ '<td><h4>' + item.lastName + '</h4></td>'
+ '<td><h4>' + item.rollNo + '</h4></td>'
+ '<td><h4>' + item.contact + '</h4></td>'
+ '<td><h4>' + item.email + '</h4></td>'
+ '<td><h4>' + item.gender + '</h4></td>'
+ '</tr>');
insideData(data);
}
},
fail: function (data) {
alert('Failed to fetch records.');
}
});
} else {
// ...
}
}
My Controller code: 我的控制器代码:
@RequestMapping(value = "/search", method = RequestMethod.POST)
@ResponseBody
public List<Object> fetchTableData(@RequestParam("tableId") String tableId) {
List<Object> userList = new ArrayList<>();
try {
System.out.println(" table id id " + tableId);
if (tableId != null) {
List<UserInfo> l = userInfoDao.findById(tableId);
userList.add(l);
}
return userList;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
As per screenshot, I only got one row with all undefined
values, what I want to do, in the image I have 7
elements, so I want to iterate and I want seven rows and their corresponding columns populated with values. 根据屏幕快照,在图像中我只有
7
元素,所以我只想一行包含所有undefined
值,所以我要进行迭代,并且希望有7行及其对应的列填充有值。 Please suggest me the solution. 请给我建议解决方案。
Well, as far as I see from your log, the structure is an array of array. 好吧,据我从您的日志中看到的,该结构是一个数组数组。 An element might be accessible using:
可以使用以下方式访问元素:
success: function (data) {
for (var i = 0; i < data[0].length; i++) { // access the first item data[0] of outer array
var item = data[0][i]; // and get the nth object
$('#applicationList > tbody').append(
// code skipped
);
insideData(data);
}
},
Why does it happen? 为什么会发生?
Because you return List<Object>
which has one element List<UserInfo>
. 因为您返回的
List<Object>
具有一个元素List<UserInfo>
。 This brief sequence of operation adds a list to a list and returns it: 这个简短的操作序列将一个列表添加到列表中并返回它:
List<Object> userList = new ArrayList<>(); // Creates a List
List<UserInfo> l = userInfoDao.findById(tableId); // Creates another of users
userList.add(l); // Adds List to List
return userList; // Returns the List to List
Since the return type is List<Object>
, you might not notice that the returned type is actually List<List<UserInfo>>
. 由于返回类型为
List<Object>
,因此您可能不会注意到返回的类型实际上是List<List<UserInfo>>
。
How to fix it? 如何解决?
There are two ways, yet I recommend you the second one: 有两种方法,但是我建议您使用第二种方法:
I suppose that you wanted to add all the elements to the outer List
and keep the flat structure. 我想您想将所有元素添加到外部
List
并保持平面结构。 For this, you have to use method List::addAll
which passes all the elements from the List
to another one. 为此,您必须使用
List::addAll
方法,它将所有元素从List
传递到另一个。 You have used List::add
which adds an element to the list as is - in your case the added element was a new entire List
and not its elements. 您已经使用
List::add
将元素按原样添加到列表中-在您的情况下,添加的元素是一个新的整个List
而不是其元素。
A better way is to return the result directly. 更好的方法是直接返回结果。 If nothing is found, return an empty
List
: 如果未找到任何内容,则返回一个空的
List
:
@RequestMapping(value = "/search", method = RequestMethod.GET) @ResponseBody public List<UserInfo> fetchTableData(@RequestParam("tableId") String tableId) { try { List<UserInfo> userList = new ArrayList<>(); System.out.println(" table id id " + tableId); if (tableId != null) { userList = userInfoDao.findById(tableId); } return userList; } catch (Exception e) { // log it, don't print the stacktrace... return Collections.emptyList() } }
What more? 还有什么?
I noticed you use the POST
method, however since you receive data from the server, you should use GET
method regardless you pass a parameter which identifies the entity to be returned. 我注意到您使用了
POST
方法,但是由于您从服务器接收数据,因此无论您传递的参数是否标识要返回的实体,都应该使用GET
方法。 From W3Schools : 从W3Schools :
GET
is used to request data from a specified resource. GET
用于从指定资源请求数据。 POST
is used to send data to a server to create/update a resource. POST
用于将数据发送到服务器以创建/更新资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.