我已将数据传递给控制器并在数组中的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"}]
答案 0 :(得分:0)
对于具有多个属性的对象和对于数组的forEach循环,您可以使用for ... in循环:
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>