首页 > 解决方案 > 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"}

我该如何解决这个问题?我怎样才能将这些数据绑定到表上???

标签: javascriptjqueryjsonangularjsspring-boot

解决方案


似乎将列表转换为字符串正在生成这种格式。

两种都可以试试

  1. 在显示数据期间,从每个数组元素中删除“EmployeeDesignation”。

或者,

  1. 制作您自己的 JSON 对象而不转换为字符串。

推荐阅读