我的数组正在返回信息并且其中包含数据(非空)但仍然存在错误:
未捕获的TypeError:无法读取属性' date_appeal_received'未定义"或"未捕获的ReferenceError:未定义date_appeal_received。
我试过了:
console.log(result.parcel[1].date_appeal_received);
console.log(result.parcel[1][date_appeal_received]);
console.log(result.parcel[1]["date_appeal_received"]);
如果我只是放console.log(result);
,它将完全返回数组,但我需要将select属性值放入输入框。
以下是代码:
PHP
<?php
require_once('../config.php');
if(isset($_GET['parcel_id'])) {
$db = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$parcel = $db->get_results("select * from parcels where parcel_id=" . $_GET['parcel_id']);
$comment_count = $db->get_var("select count(*) as count from comments where parcel_id=" . $_GET['parcel_id']);
echo json_encode(array('parcel'=>$parcel,'comment_count'=>$comment_count));
}
?>
jQuery:
$.ajax({
url: "classes/get-apn-count-comments.php?parcel_id=" + parcel_id,
type: "GET",
data: { parcel_id : parcel_id },
dataType: 'json',
error: function(SMLHttpRequest, textStatus, errorThrown){
alert("An error has occurred making the request: " + errorThrown);
},
success: function(result){
//do stuff here on success
//console.log(result);
$('#ViewComments').val('View ' + result.comment_count + ' Comments');
if (result.parcel[0].apn != null){ $('#ParcelNumber').html(result.parcel[0].apn)
} else { $('#ParcelNumber').html(" "); }
console.log(result.parcel[1].date_appeal_received);
/*if (result.parcel[1].date_appeal_received != null) { $('#DateAppealReceived').html(result.parcel[1].date_appeal_received);
} else { $('#DateAppealReceived').html(" "); }
if (result.parcel[2].date_appeal_received != null) { $('#BosMeetingDate').html(result.parcel[2].bos_meeting_date);
} else { $('#BosMeetingDate').html(" "); }
if (result.parcel[3].late_returns_date != null) { $('#LateReturnsDate').html(result.parcel[3].late_returns_date);
} else { $('#LateReturnsDate').html(" "); }
if (result.parcel[4].determination_notice_sent_date != null) { $('#DeterminationNoticeSent').html(result.parcel[4].determination_notice_sent_date);
} else { $('#DeterminationNoticeSent').html(" "); }
if (result.parcel[5].final_determination != null) { $('#FinalDetermination').html(result.parcel[5].final_determination);
} else { $('#FinalDetermination').html(" "); }
if (result.parcel[6].analysis_recommendation != null) { $('#AnalysisRecommendation').html(result.parcel[6].analysis_recommendation);
} else { $('#AnalysisRecommendation').html(" "); }
if (result.parcel[7].email_address != null) { $('#EmailAddress').html(result.parcel[7].email_address);
} else { $('#EmailAddress').html(" "); }
if (result.parcel[8].phone_number != null) { $('#PhoneNumber').html(result.parcel[8].phone_number);
} else { $('#PhoneNumber').html(" "); }*/
}
});
数组结果:
{
"parcel": [{
"parcel_id": "89415",
"tax_year": "2012",
"apn": "134-44-114",
"vin": null,
"owner_name": "BARRERA DIANE",
"in_care_of": null,
"mailing_address_1": "2402 W RIVIERA DR",
"mailing_address_2": null,
"city_state_zip": "TEMPE AZ 85282",
"suite": null,
"country": null,
"initial_mail_date": "2012-05-11",
"initial_return_date": null,
"final_mail_date": "2012-07-13",
"final_return_date": null,
"reclass_mail_date": "2012-09-11",
"bos_meeting_date": "2012-10-17",
"date_appeal_received": "2012-09-18",
"late_returns_date": null,
"determination_notice_sent_date": null,
"response_code": "No Response",
"email_address": "dianefm@cox.net",
"phone_number": "4807734925",
"notification_type": null,
"analysis_recommendation": "3",
"final_determination": "A",
"account": null,
"revised_legal_class": null,
"other": null,
"change_address_request": null,
"determination_notice_sent": null,
"appeal_address_match": null,
"situs_address_1": "2402 W RIVIERA DR",
"situs_address_2": null,
"situs_city_state_zip": "TEMPE AZ 85282",
"owner_city": null,
"owner_state": null,
"owner_zip": null,
"situs_city": null,
"situs_state": null,
"situs_zip": null,
"penalty_amount": null,
"penalty_mail_date": null,
"penalty_appeal_recieved_date": null,
"penalty_bos_meeting_date": null,
"penalty_determination_notice_sent_date": null,
"penalty_determination": null
}],
"comment_count": "9"
}
答案 0 :(得分:1)
数组中的第一个索引是0
,而不是1
。您的parcel
数组中只有一个条目:result.parcel[0]
。如果你想要它的date_appeal_received
属性,那就是:
console.log(result.parcel[0].date_appeal_received);
但是您的success
函数以此代码开头:
$('#ViewComments').val('View ' + result.comment_count + ' Comments');
if (result.parcel[0].apn != null){ $('#ParcelNumber').html(result.parcel[0].apn)
} else { $('#ParcelNumber').html(" "); }
console.log(result.parcel[1].date_appeal_received); // <=== Problem line
上面的“问题行”没有任何防范,因此它将始终运行。如果result.parcel
中只有一个条目,result.parcel[1]
为undefined
,那么尝试从中读取date_appeal_received
属性会导致错误。选中result.parcel.length
以确定它是> 1
,或者由于result.parcel
中的条目是对象,您可以使用
if (result.parcel[1]) {
// ...use result.parcel[1].whateverPropertyHere
}
如果您只想尝试与result.parcel[0]
位于同一对象(apn
)上的其他属性,请继续使用result.parcel[0]
:
console.log("APN: " + result.parcel[0].apn);
console.log("Date received: " + result.parcel[0].date_appeal_received);
// ...and so on
或者保存所有输入和冗余查找:
var parcel = result.parcel[0];
console.log("APN: " + parcel.apn);
console.log("Date received: " + parcel.date_appeal_received);
// ...and so on
旁注:
这也适用于从第一个条目获取属性:
console.log(result.parcel[0]["date_appeal_received"]);
...因为在JavaScript中,您可以使用点表示法和文字属性名称(foo.bar
)或括号表示法和字符串属性名称({{1)来访问属性}})。