javascript - jquery-无法从我通过ajax调用获得的对象类型响应中检索数据
问题描述
我有一张桌子的页面。我正在尝试通过 ajax 调用从 api 获取数据并检索该数据并尝试将其显示在页面的表格中。所以,我需要 key 和 value 两者。我想将键设置为列名(列的标题)和行的值。
但我无法从响应中检索我的数据,也无法将其绑定到 html 页面中。
这是我的控制器:
public ResponseEntity<Object> findPaginated(
@RequestParam("page") int page, @RequestParam("size") int size) throws NotFoundException {
JsonParser parser = new BasicJsonParser();
HttpHeaders headers = new HttpHeaders();
ApiResponse response = new ApiResponse(false);
Page<EmployeeDesignation> resultPage = designationService.findPaginated(page, size);
List<EmployeeDesignation> parseList = resultPage.getContent();
response.setSuccess(parser.parseList(String.valueOf(parseList)));
headers.add("totalelement", String.valueOf(resultPage.getTotalElements()));
headers.add("totalpages", String.valueOf(resultPage.getTotalPages()));
System.err.println ( " in controller ");
return ResponseEntity.ok()
.headers(headers)
.body(response);
}
这是我的 ajax 调用格式(.js 文件):
function getList() {
$http({
url: "/api/designation/designations/get?",
method: 'GET',
params:{ page: 2, size: 2 }
}).then(function successCallback(response) {
let info = Object.values(response.data.data);
let check = Object.values(info);
console.log("keys::::" + check);
getArray(check);
}
, function errorCallback(data) { });};
function getArray($products){
jQuery($products).each(function(i, obj) {
jQuery('div#check').append(obj.name + '::::::contains ::::::' +obj.designationRefId);
});
}
但是当我在 page 上附加数据时,它显示未定义。
这是我的 .json 格式文件:
{
"success": true,
"message": null,
"data": [
"EmployeeDesignation{id=75928ab9-7c97-442d-b777-dc32ca9ef49d, version=0, refId=0, designationRefCode='2', name='programmer'}",
"EmployeeDesignation{id=57305c1f-bca0-4a56-ba19-5784c4461f5b, version=0, refId=0, designationRefCode='2', name='designer'}"
]
}
这是console.log(JSON.stringyfy(response))的输出:
{"data":{"success":true,"message":null,"data":["EmployeeDesignation{id=75928ab9-7c97-442d-b777-dc32ca9ef49d, version=0, refId=0, designationRefCode='2', name='programmer'}","EmployeeDesignation{id=57305c1f-bca0-4a56-ba19-5784c4461f5b, version=0, refId=0, designationRefCode='2', name='designer'}"]},"status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/designation/designations/get?","params":{"page":2,"size":2},"headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"","xhrStatus":"complete"}
我该如何解决这个问题?我怎样才能将这些数据绑定到表上???
解决方案
似乎将列表转换为字符串正在生成这种格式。
两种都可以试试
- 在显示数据期间,从每个数组元素中删除“EmployeeDesignation”。
或者,
- 制作您自己的 JSON 对象而不转换为字符串。
推荐阅读
- linux - 通过取变量值循环
- arrays - 如何在bash中从csv转换为数组
- elasticsearch - Kibana,匿名访问,仅限仪表板
- laravel - laravel 在功能上获得价值请求?
- vba - 使用 excel VBA 在 powerpoint 中更改页眉/页脚日期和时间的格式
- css - 整个网络客户端背景
- p2p - Bittorrent 在多个对等点之间维护状态
- python - 根据字段的唯一值在 PySpark 数据帧中生成 UUID
- bash - 在 Mac OSX 中相当于 cut -c ... --output-delimeter=','?
- python - 如何在 Python 中使用 Pandas 有效地将数据框重组为日期时间条目?