首页 > 解决方案 > 在表格ajax上显示json

问题描述

如何使用 ajax 显示以下代码问题是,我无法显示 chequebook.statuses.name 我只需要打印名称所有数据都可以,但名称

ajax调用如下

success: function(data) {

            $("#edit-error-bag").hide();

            $.each(data, function(i, chequeBook){ 
            var test = null;  
                 $.each(chequeBook.statuses , function(k , v ){ 
                           test = v[1]    
                 })

            $("#tbody").html("<tr><td>" + chequeBook.id + "</td><td>" + 
                chequeBook.account_id + "</td><td>" + chequeBook.number_of_books 
                 + "</td><td>" + chequeBook.number_of_leaves +   "</td><td>" +
                  ((chequeBook.currency_id == 1 ) ? 'USD' : 'USD') +  "</td><td>" +/*
                  ((chequeBook.statuses != 'undefined') ? 'Fine' : 'Not Fine' )*/ 

json数据的内容

       {"error":false,"chequeBookRequest":{
    "id":50,10:42:56","account_id":46764995,
    "modifier_id":null,
    "statuses":{"id":4,"name":"Collected","description":"Cheque book is collected by a customer","created_at":"2020-02-18 10:27:05","updated_at":"2020-02-18 10:27:05","deleted_at":null},
                            "modifiers":null}
                            }

上面的代码不起作用我尝试了一切似乎都没有工作从到到ChequeBook.statuses.name没有chequeBook[statuses][name]工作我只是尝试显示状态的名称而不是整个对象。chequeBook.statuses[0].namechequeBook.statuses['name']

在此处输入图像描述

标签: jqueryajaxlaravel

解决方案


您的响应状态不是数组。它们是一个对象。

您当前的尝试表明您正在尝试像访问数组一样访问它。

假设chequeBook是一个chequeBookRequest在您的 JSON 响应中引用的变量,那么您可以使用chequeBook.statuses.name.


推荐阅读