Php Ajax Json数据-如何访问返回的Json数据
当我点击Php Ajax Json数据-如何访问返回的Json数据,php,json,ajax,codeigniter,Php,Json,Ajax,Codeigniter,当我点击列表产品(发票号)时,我会打开一个可折叠的表格,列出与发票号匹配的产品 我的视图文件: <script> $(document).ready(function(){ $(".listproduct").click(function(){ var value = $(this).text(); alert(value); $.post('<?=site_url("purchase_controller/getprod
列表产品
(发票号)时,我会打开一个可折叠的表格,列出与发票号匹配的产品
我的视图文件:
<script>
$(document).ready(function(){
$(".listproduct").click(function(){
var value = $(this).text();
alert(value);
$.post('<?=site_url("purchase_controller/getproductlist"); ?>', {data:value},function (result) {
alert(result[1][1]["invoiceno"]);
for(i=1;i<result["count"];i++){ //loop trough elements
$('#products tr:last').after('<tr><td>' + result[i]['invoiceno'] + '</td><td>' + result[i]['productname'] + '</td><td>' + result[i]['price'] + '</td></tr>');
}
});
$(".collapse").collapse('toggle');
});
});
</script>
<table id="products">
<tbody>
<tr>
<td >Invoice No</td>
<td>productname</td>
<td> quantity</td>
<td> price</td>
<td> amount</td>
</tr>
</tbody>
</table>
调试时的我的输出-针对ajax的响应:
{ "records": [
{"id":"39","invoiceno":"55","price":"30000","quantity":"2", "amount":"60000","productname":"Tab"},
{"id":"41","invoiceno":"55", "price":"200","quantity":"4","amount":"800","prod uctname":"zdsfS"}
],
"count":2
}
但这是我得到的错误:
TypeError: result[1] is undefined
alert(result[1][1]["invoiceno"]);
jQuery的parseJSON()
函数将解析JSON字符串
var resultObj = $.parseJSON( result );
alert(resultObj.records[1].invoiceno);
参考:
这是工作!我刚刚得到:)您需要解析json,然后您可以使用对象访问发票号。请参阅我的代码片段。请参阅json的更多详细信息
在JavaScript中创建JSON对象
var text='{“记录”:[{“id”:“39”,“发票号”:“55”,“价格”:“30000”,“数量”:“2”,“金额”:“60000”,“产品名称”:“Tab”},{“id”:“41”,“发票号”:“55”,“价格”:“200”,“数量”:“4”,“金额”:“800”,“产品名称”:“zdsfS”}],“计数”:2};
var obj=JSON.parse(文本);
document.getElementById(“demo”).innerHTML=
对象记录[1]['invoiceno']+“
”;
那是因为它是未定义的
<代码>警报(结果.记录[0].发票编号)代码>。
var resultObj = $.parseJSON( result );
alert(resultObj.records[1].invoiceno);
alert(result['records'][1]["invoiceno"]);