gpt4 book ai didi

php - 打印从 Ajax 响应收到的多维数组

转载 作者:行者123 更新时间:2023-12-01 08:34:01 24 4
gpt4 key购买 nike

我已使用以下代码提交了表单

$(document).ready(function() {
$.ajax({
type: "POST",
url: "https://cors-anywhere.herokuapp.com/https://api.digitallocker.gov.in/public/oauth2/1/files/issued",
headers:{
'Authorization' : 'Bearer '+$('#access_token').val()
},

dataType: "json",
success: function (data) {
console.log(data);


},
error: function(xhr, status, error) {
alert(xhr.responseText);
}
});
});

我在控制台中收到了以下输出。

{items: Array(6), resource: "D"}
items: Array(6)
0:
date: "2x-x-xxx"
description: "Aadhaar Card"
doctype: "ADHAR"
issuer: "Aadhaar, Unique Identification Authority of India"
issuerid: "in.gov.uidai"
mime: "application/pdf"
name: "Aadhaar Card"
parent: ""
size: ""
type: "file"
uri: "in.gov.uidai-ADHAR-xxxxxxx"
__proto__: Object

我想将上述输出打印为 HTML 中的表格。

到目前为止我已经尝试过

jQuery(data).each(function(i, obj) {
jQuery('#doc_list').append("<tr><td>"+obj.items['date'] + ' </td><td>' + obj.description + "</td></tr>");
});

最佳答案

您似乎想要循环 data.items,而不仅仅是 data

经过一些重构,变成:

data.items.forEach(item => {
$('#doc_list').append(
$('<tr>')
.append($('<td>').text(item.description))
.append($('<td>').text(item.date))
// append other columns as required
);
});

演示:

const dataJson = `
{
"items": [{
"date": "2x-x-xxx",
"description": "Aadhaar Card",
"doctype": "ADHAR",
"issuer": "Aadhaar, Unique Identification Authority of India",
"issuerid": "in.gov.uidai",
"mime": "application/pdf",
"name": "Aadhaar Card",
"parent": "",
"size": "",
"type": "file",
"uri": "in.gov.uidai-ADHAR-xxxxxxx"
}, {
"date": "3x-x-xxx",
"description": "Somethingelse Card",
"doctype": "SOMETHINGELSE",
"issuer": "Somethingelse, Unique Identification Authority of India",
"issuerid": "in.gov.uidai",
"mime": "application/pdf",
"name": "Somethingelse Card",
"parent": "",
"size": "",
"type": "file",
"uri": "in.gov.uidai-SOMETHINGELSE-xxxxxxx"
}
]}`;

const data = JSON.parse(dataJson);

data.items.forEach(item => {
$('#doc_list').append(
$('<tr>')
.append($('<td>').text(item.description))
.append($('<td>').text(item.date))
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="doc_list">
</table>

关于php - 打印从 Ajax 响应收到的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59016543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com