我有一个简单的ajax json请求。
这是客户端javascript:
function ajax(){
//alert('hello');
$.ajax({
url: '../ajax/goal_ajax.php',
dataType: 'json',
success: function( data ){
// success! :D
alert('hello');
}, error: function( data ){
// data.responseText is what you want to display, that's your error.
alert('die');
}
})
//progressBar.set('value',data.total);
//document.getElementById('txtCDInfo').innerHTML=txt;
}
这是php处理程序:
<?php
include ("../includes/db_con.php");
$itemResults = mysql_query("SELECT `sold_for` FROM `items` WHERE `item_did_sell`='1'") or die();
$mIResults = mysql_query("SELECT `mi_price`, `mi_total_sold` FROM `misc_items` WHERE `mi_total_sold`>'1'") or die();
$donationResults = mysql_query("SELECT `amount` FROM `donations`") or die(mysql_error());
$total = 0;
$itemTotal = 0;
$mITotal = 0;
$donationTotal = 0;
while($row = mysql_fetch_assoc($itemResults)){
$itemTotal += $row['sold_for'];
$total += $itemTotal;
}
while($row = mysql_fetch_assoc($mIResults)){
$mITotal += ($row['mi_price'] * $row['mi_total_sold']);
$total += $mITotal;
}
while($row = mysql_fetch_assoc($donationResults)){
$donationTotal += $row['amount'];
$total += $donationTotal;
}
header("Content-Type: application/json");
echo json_encode(array("items" => $itemTotal, "mitems" => $mITotal, "donations" => $donationTotal, "total" => $total));
include ("../includes/db_discon.php");
?>
当我在客户端页面上调用函数ajax()
时,会出现一个警告框,显示die
,显然意味着请求失败。但是,我没有在页面上收到404错误,所以它找到了php处理程序。
php脚本echo:
{"items":1000,"mitems":0,"donations":0,"total":1000}
编辑:
data.responseText
返回{"items":1000,"mitems":0,"donations":0,"total":1000}