gpt4 book ai didi

javascript - 如何在ajax调用中获取内部键值和数据?

转载 作者:行者123 更新时间:2023-12-03 04:26:46 25 4
gpt4 key购买 nike

我在 jquery 中尝试了以下代码行

              <h5>Marks per Subjects</h5>
<ul id="subjectsMarks">

</ul>


$.ajax({
url: 'index.php?action=fetchStudentIfResultFound',
type: 'POST',
dataType: 'JSON',
...
...

for(i=0; i<data[1].length; i++)
{
$("#subjectsMarks li").html(key[1][i] + ':' + data[1][i]+ "<br/>"+data[1]["Status"]);
}
});

在控制台中数据就像

0:目的1:数组(4)0:数组(1)1:数组(2)0:目的物理:“150.00/200”地位:“经过”原型(prototype):目的1:目的科学:“400.00/600”地位:“经过”原型(prototype):目的…………

我希望 li 中的数据应该看起来像

每个科目的分数

       Physics: 150.00/200
Status : Pass

Science: 400.00/600
Status: Pass

请帮助我!!!

最佳答案

从后端修改数据并向数据添加键,如下所示,可以很容易地知道哪里必须循环并且不需要知道何时停止

var data: {
student: {
"_id": {
"$id": "5715f58299fbad800100003a"
},
"student_id": 36,
"‌​registration_temp_pe‌​rm_no": "453654",
"rol‌​l_no": "31",
"admissio‌​n_date": "04/12/2016"‌​,
"first_name": "Momin‌​",
"middle_name": "Bas‌​hir",
"last_name": "Ra‌​ther",
"dob": "7/9/199‌​5",
"gender": "Male",
"‌​blood_group": "",
"bir‌​th_place": "",
"nation‌​ality": "",
"language"‌​: "",
"religion": "Isla‌​m"...
},
marks: {
subjects: [
{
"Physic‌​s": "150.00/200",
"Sta‌​tus": "Pass"
},
{
"Scien‌​ce": "400.00/600",
"St‌​atus": "Pass"
}
],
"Tot‌​al Marks": "550/800",
"Grade": "Ist",

}
}

尝试使用下面的代码循环

//Without modify json from backend
var allSubjects=data[1][1];

//With modify json from backend
var allSubjects=data[1][1]['Sub‌​jects'];

for(var i in allSubjects){


var keysOfObject=Object.Keys(allSubjects[i]);

$("#subjectsMarks li").html(keysOfObject[0] + ':' + allSubjects[i][keysOfObject[0]]+ "<br/>"allSubjects[i]["Status"]);
/* //console.log(allSubjects[i].keysOfObject[0], keysOfObject[0])
console.log(allSubjects[i]);*/


}

第二版循环

for(var i in allSubjects){

var subject='';
for(var key in allSubjects[i]){
subject=key;
break;
}
//var keysOfObject=//Object.Keys(allSubjects[i]);

$("#subjectsMarks li").html(subject + ':' + allSubjects[i][subject]]+ "<br/>"allSubjects[i]["Status"]);

console.log(allSubjects[i][subject], subject)

}

关于javascript - 如何在ajax调用中获取内部键值和数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43672673/

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