我有一个cakephp代码与数据库一起工作,以搜索给定的卡号,并返回余额。 jQuery代码看起来像这样。CakePHP的jquery调试信息里面的ajax响应
function subm(event){
$.ajax({
type: "POST",
//contentType: "application/json; charset=utf-8",
dataType:'json',
url:"\/balances\/balance",
data:$("#button_check_balance").closest("form").serialize(),
cache: false,
beforeSend:function(){
$("#loadingDiv").show(1000);
},
success:function (data, textStatus,xhr) {
$("#loadingDivision").hide(1000);
alert("balance is "+data.balance);
return false;
},
//failure: function(msg){alert(msg);},
error: function(xhr, ajaxOptions, thrownError){
$("#loadingDivision").hide(1000);
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
console.log(xhr.responseText);
},
/*complete: function(){
alert("complete");
},*/
});
我有balancesController和balance.ctp文件,控制器逻辑看起来像这样。
function balance() {
$message = "";
$error = "";
$this->layout = 'ajax'; //layout should be ajax based on
Configure::write('debug', 0);
//gets the submitted card number
$card_id = $this->data['balances']['cardId']; //entered card id of the emp
if (!empty($this->data)) {
$this->header('Content-Type: application/json');
try {
$card = $this->Card->getBalance($card_id);
} catch (Exception $e) {
$error = "balance not available";
$resp = json_encode(array('error' => $error));
echo $resp;
exit;
}
if ($this->RequestHandler->isAjax()) {
$this->autoRender = $this->layout = false;
$resp = json_encode(array('cardData' => $cardObj);
echo $resp;
exit;
}
}
}
个问题,我有是 - 当出现平衡不可用错误“我得到在我的Ajax响应蛋糕DEBUG INFOMATION。”
如 - 当我尝试使用“xhr.responseText”我得到了长时间的输出由 <pre class="cake-debug">
访问XHR对象错误函数中对阿贾克斯$事件 ..........和这个唯一的结局我得到了我编码成json的错误。我已经使用Configure :: write('debug',1);和Configure :: write('debug',0);没有任何运气。你可以看到我使用Configure :: write('debug',0);在我的控制器功能顶部以及.. 请指教我解决这个问题。所有的输入都非常感谢。
嗨JohnP,首先thanx很多回复。我对cakephp相当陌生,因为我一直在Java背景下工作一段时间..我期望PHP异常处理是在java的水平......我现在明白的是它不能达到这个水平。我已经添加了一个代码来使用'$ cardObj = $ this-> Card-> getNewBalance($ card_id);'然后访问返回的对象的属性..使用'$ name = $ cardObj [0] ['名称']'....当没有可用的搜索结果....访问cardObject数组失败 – Sanath 2011-03-07 08:38:47
@Sanath,PHP确实有异常处理,但CakePHP不使用异常。你可以添加你在pastebin上得到的错误吗?很可能你正在访问一个数组元素而不检查它是否存在。 Cake没有找到可以返回false,从而导致代码中断 – JohnP 2011-03-07 08:38:49