JSON:
[
{
"ID":"25",
"Serial":"1",
"Purchase_id":"8",
"Item":"23",
"Unit":"1",
"HSN":"84212120",
"Quantity":"10",
"Purchase_rate":"100",
"Discount":"10",
"Discount_2":"5",
"Net_rate":"85.5",
"CGST_Percentage":"9",
"SGST_Percentage":"9",
"IGST_Percentage":"0",
"Rate_after_tax":"100.89",
"CGST":"76.95",
"SGST":"76.95",
"IGST":"0",
"Net_amount_without_tax":"855",
"Net_amount":"1008.9"
}
]
jQuery的:
$.ajax({
method: "POST",
url: formsubmission,
data: data,
success: function(response) {
var data = JSON.parse(response);
alert(data.ID);
}
})
任何人都可以帮助我,为什么警报会带来未定义的消息。如何解决i8t问题?如何提醒25而不是未定义?
答案 0 :(得分:1)
对象位于已解析响应的结果0
的索引Array
处。您可以使用括号表示法来引用JavaScript对象<{p>}的索引0
处的元素
let response =`[{"ID":"25","Serial":"1","Purchase_id":"8","Item":"23","Unit":"1","HSN":"84212120","Quantity":"10","Purchase_rate":"100","Discount":"10","Discount_2":"5","Net_rate":"85.5","CGST_Percentage":"9","SGST_Percentage":"9","IGST_Percentage":"0","Rate_after_tax":"100.89","CGST":"76.95","SGST":"76.95","IGST":"0","Net_amount_without_tax":"855","Net_amount":"1008.9"}]`;
let data = JSON.parse(response);
alert(data[0].ID);
答案 1 :(得分:0)
var response =`[{"ID":"25","Serial":"1","Purchase_id":"8","Item":"23","Unit":"1","HSN":"84212120","Quantity":"10","Purchase_rate":"100","Discount":"10","Discount_2":"5","Net_rate":"85.5","CGST_Percentage":"9","SGST_Percentage":"9","IGST_Percentage":"0","Rate_after_tax":"100.89","CGST":"76.95","SGST":"76.95","IGST":"0","Net_amount_without_tax":"855","Net_amount":"1008.9"}]`;
var data = JSON.parse(response);
for (var i = 0; i < data.length; i++) {
alert(data[i].ID);
}
&#13;