使用显示数据在循环中使用ajax数组响应
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用显示数据在循环中使用ajax数组响应相关的知识,希望对你有一定的参考价值。
我已经将数据传递给控制器并在数组中的ajax中获取数据,因此如何在ajax中使用循环并显示数据。
我的ajax回应是
[{"description":"Spring (03/01/2017 - 30/03/2017) - Training Fees","attandence_count":"0","pay":"u00a3159.00"},{"description":"Spring (03/01/2017 - 30/03/2017) - Training Fees","attandence_count":"0","pay":"u00a3159.00"},{"description":"Spring (03/01/2017 - 30/03/2017) - Training Fees","attandence_count":0,"pay":"u00a3159.00"},{"description":"Spring (03/01/2017 - 30/03/2017) - Training Fees","attandence_count":0,"pay":"u00a3159.00"}]
答案
对于具有多个属性的对象,可以使用for ... in循环,对于数组,可以使用forEach循环:
const response = [{"description":"Spring (03/01/2017 - 30/03/2017) - Training Fees","attandence_count":"0","pay":"u00a3159.00"},{"description":"Spring (03/01/2017 - 30/03/2017) - Training Fees","attandence_count":"0","pay":"u00a3159.00"},{"description":"Spring (03/01/2017 - 30/03/2017) - Training Fees","attandence_count":0,"pay":"u00a3159.00"},{"description":"Spring (03/01/2017 - 30/03/2017) - Training Fees","attandence_count":0,"pay":"u00a3159.00"}];
response.forEach(session => {
const ul = document.createElement("ul");
for (const key in session) {
const li = document.createElement("li");
li.innerhtml= `${key}: ${session[key]}`;
ul.appendChild(li);
}
document.getElementById('session').appendChild(ul);
})
<div id="session"></div>
以上是关于使用显示数据在循环中使用ajax数组响应的主要内容,如果未能解决你的问题,请参考以下文章
在 JavaScript 中使用 ajax 提交和显示表单数据