我在我的php函数中使用json_encode将数据库中的多个结果发送到ajax调用。
我以下列格式获得结果..
[
{
"id": 24,
"title": "Mr",
"first_name": "Patrick",
"last_name": "Vinc",
"gender": "male",
"email": "nupur@gmail.com",
"password": "$2y$10$yCyGBOtX6kF3ghy/k8YuXe4wR9W5hYtTGDkl5trTEd7.s5LntOQ.u",
"phone_type": null,
"phone_number": "0000000000",
"pager_number": "00000000000000",
"address_line_1": "",
"address_line_2": "",
"city": "",
"postal_code": "",
"province": "BC",
"country": null,
"emc_contact": "",
"emc_phone": "000000000000000000",
"emc_relation": "",
"passcode": "",
"locker": "999999",
"combination": "abc567",
"its_username": null,
"its_password": null,
"dictation_number": null,
"emailed": 1,
"signed": 0,
"student_num": "12345634",
"level": "Default",
"persist_code": "",
"activated_at": "2014-08-23 16:04:18",
"program": null,
"school": "",
"service": "",
"undergrad_year": null,
"undergrad_level": null,
"activated": 1,
"activation_code": "",
"undergrad_text": null,
"cpso_num": 0,
"start_date": "2014-08-01",
"end_date": "2014-08-31",
"learner_start_date": "0000-00-00",
"learner_end_date": "0000-00-00",
"vacation_start_date": "0000-00-00",
"vacation_end_date": "0000-00-00",
"physician": "1",
"affiliates": null,
"mask": "",
"mask_fit_month_year": "",
"learner_type": null,
"status": 1,
"last_login": "0000-00-00 00:00:00",
"reset_password_code": "",
"permissions": "",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2014-08-22 21:27:17"
}
]
如何访问此数据?我想得到名字和姓氏。
答案 0 :(得分:0)
希望这有帮助
$json = json_decode($jsondata);
foreach ($json as $item)
{
echo $item->first_name;
echo $item->last_name;
}
在Javascript中执行相同操作:
for(var i=0;i<data.length;i++){
var item=data[i];
console.log(item['first_name']);//Or item.first_name also works
console.log(item['last_name']);//
}
答案 1 :(得分:-1)
我认为您正试图通过AJAX请求将JSON检索到PHP应用程序吗?
您需要向PHP应用程序发出一个AJAX请求,该请求应该适当地响应(使用JSON数据)
我相信你需要这样的东西:
$.post( "ajax/test.html", function( data ) {
for (item in data){
console.log(item.first_name);
console.log(item.last_name);
}
});
确保您实际上回应了PHP应用程序的响应。