如何使用 AJAX 在 HTML 表中显示带有数字键的 JSON 值
我是 Web 开发新手,一直在尝试使用来自不同 API 的数据构建表格,但遇到了对象内包含数字键的响应问题。
我得到了在日志中显示的响应,但无法将数据获取到表中。
我重用了之前测试的代码(成功),但在本次测试中,键是数字。这是响应:
{"345017":"Simo","345019":"Simola","345020":"email","360241":"This is a comment","360858":"CEO","360859":"Test Company AS","360895":"Mr.","362692":"Football"}
这是我在上一次成功测试中重复使用的代码:
window.onload=function(){
document.getElementById('button').addEventListener('click', loadRest);
function loadRest() {
window.onload=function(){
document.getElementById('button').addEventListener('click', loadRest);
function loadRest() {
var myArray = []
buildTable(myArray)
$.ajax({
method: 'GET',
url: '(url here)',
Accept: 'application/json',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer (token here)');
},
success:function(response){
myArray = response
buildTable(myArray)
console.log(myArray)
}
})
function buildTable(object){
var table = document.getElementById('myTable')
{
for (var i = object; i < object.length; i++){
var row = `<tr>
<td>${object[i][345017]}</td>
<td>${object[i][345019]}</td>
<td>${object[i][345020]}</td>
<td>${object[i][360241]}</td>
<td>${object[i][360858]}</td>
<td>${object[i][360859]}</td>
<td>${object[i][360895]}</td>
<td>${object[i][362692]}</td>
</tr>`
table.innerHTML += row
}
}}}}
此代码与成功代码之间的区别在于,成功代码以字母作为键,例如 ${ items[I].name}
并且它是一个数组而不是一个对象。
任何帮助将不胜感激。
I'm new to web development and I´ve been trying out building tables with data from different API´s but have run into a problem with a response that has numeric keys within the object.
I get the response to show in log but can not get the data to a table.
I reused the code from a previous test (that was successful), but in this test the keys are numeric. Here is the response:
{"345017":"Simo","345019":"Simola","345020":"email","360241":"This is a comment","360858":"CEO","360859":"Test Company AS","360895":"Mr.","362692":"Football"}
And here is the code that I've reused from the previous successful test:
window.onload=function(){
document.getElementById('button').addEventListener('click', loadRest);
function loadRest() {
window.onload=function(){
document.getElementById('button').addEventListener('click', loadRest);
function loadRest() {
var myArray = []
buildTable(myArray)
$.ajax({
method: 'GET',
url: '(url here)',
Accept: 'application/json',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer (token here)');
},
success:function(response){
myArray = response
buildTable(myArray)
console.log(myArray)
}
})
function buildTable(object){
var table = document.getElementById('myTable')
{
for (var i = object; i < object.length; i++){
var row = `<tr>
<td>${object[i][345017]}</td>
<td>${object[i][345019]}</td>
<td>${object[i][345020]}</td>
<td>${object[i][360241]}</td>
<td>${object[i][360858]}</td>
<td>${object[i][360859]}</td>
<td>${object[i][360895]}</td>
<td>${object[i][362692]}</td>
</tr>`
table.innerHTML += row
}
}}}}
The differences between this and the successful one is that the successful one had letters as keys, for example <td>${items[I].name}</td>
and that it was an array instead of an object.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试这个代码
结果
you can try this code
result