2011-03-04 58 views
0

我有一个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);在我的控制器功能顶部以及.. 请指教我解决这个问题。所有的输入都非常感谢。

回答

0

如果您收到调试消息,这意味着您的代码中有错误,您应该修复它而不是隐藏它。阅读错误信息(或将其粘贴到此处)以找出并解决问题

您正在使用throw/catch。 Cake通常不会使用异常来处理错误,除非您已经专门编写了模型来引发异常,否则将不会捕获错误状态。可能$卡只是返回false。

请将您的错误粘贴到这里,如果它真的很长,请在pastebin上粘贴。

+0

嗨JohnP,首先thanx很多回复。我对cakephp相当陌生,因为我一直在Java背景下工作一段时间..我期望PHP异常处理是在java的水平......我现在明白的是它不能达到这个水平。我已经添加了一个代码来使用'$ cardObj = $ this-> Card-> getNewBalance($ card_id);'然后访问返回的对象的属性..使用'$ name = $ cardObj [0] ['名称']'....当没有可用的搜索结果....访问cardObject数组失败 – Sanath 2011-03-07 08:38:47

+0

@Sanath,PHP确实有异常处理,但CakePHP不使用异常。你可以添加你在pastebin上得到的错误吗?很可能你正在访问一个数组元素而不检查它是否存在。 Cake没有找到可以返回false,从而导致代码中断 – JohnP 2011-03-07 08:38:49