HTML:
<tr>
<td colspan="6">
<table border="1" width="100%" align="center" class="info">
</table>
</td>
</tr>
jquery:
$.ajax({
type: "POST",
url: "billingprocess.php",
data: dataString,
success: function (msg) {
alert(msg);
for (var x = 0; x < msg.length; x++) {
var div = $("<tr>").appendTo(".info");
$("<td>").addClass("cols").text(msg[x].B_account).appendTo(div);
$("<td>").addClass("cols").text(msg[x].B_consumer).appendTo(div);
$("<td>").addClass("cols").text(msg[x].month).appendTo(div);
$("<td>").addClass("cols").text(msg[x].net_amount).appendTo(div);
$("<td>").addClass("cols").text(msg[x].due_date).appendTo(div);
$("<td>").addClass("cols").text(msg[x].gross_amount).appendTo(div);
}
}
});
php代码:
if ($rc > 0) {
$i = 0;
while ($i < $rc) {
$r = mysql_fetch_assoc($squery);
$temp[$i] = array("B_account" = > $r['B_account'],
"B_consumer" = > $r['B_consumer'],
"month" = > $r['month']."-".$r['year'],
"net_amount" = > $r['net_amount'],
"due_date" = > $r['due_date']."-".$r['month']."-".$r['year'],
"gross_amount" = > $r['gross_amount']);
$i++;
}
echo json_encode($temp);
}
JSON格式返回数据:
[
{
"B_account": "1014052480080",
"B_consumer": "LA712722",
"month": "Nov-11",
"net_amount": "2107",
"due_date": "30-Nov-11",
"gross_amount": "2282"
},
{
"B_account": "1014052480080",
"B_consumer": "LA712722",
"month": "Dec-11",
"net_amount": "1217",
"due_date": "30-Dec-11",
"gross_amount": "1316"
},
{
"B_account": "1014052480080",
"B_consumer": "LA712722",
"month": "Jan-12",
"net_amount": "737",
"due_date": "31-Jan-12",
"gross_amount": "795"
}
]
这是我的整个代码我认为脚本中有错误,但是有人解决了这个问题吗?
答案 0 :(得分:1)
在你的success
回调中
success: function(msg) { // msg is json string here
for (var x = 0; x < msg.length; x++) {...}
}
但在迭代之前,您应该将返回的数据转换为object
,因为您只有json
字符串,并且没有定义dataType
因此可以使用{{ 1}}
$.parseJSON
然后替换以下
var obj = $.parseJSON(msg);
与
for (var x = 0; x < msg.length; x++) {...}