ajax 响应中的 Cakephp jquery 调试信息

发布于 2024-10-20 06:46:37 字数 2304 浏览 2 评论 0原文

我有一个 cakephp 代码,可以与数据库一起搜索给定的卡 ID 号并返回余额。 jquery 代码如下所示。

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 响应中获取蛋糕调试信息。”

例如 - 当我尝试在 $.ajax 事件的错误函数内访问 xhr 对象时 使用“xhr.responseText”我得到的长输出包括

........................并且在最后我只得到了我已编码到json中的错误。
{“错误”:“错误......”}
我已经使用了Configure::write('debug', 1);和配置::写入('调试', 0);没有任何运气。正如你所看到的,我使用了Configure::write('debug', 0);也在我的控制器功能的顶部..
请建议我解决这个问题。非常感谢您的所有意见。

I have a cakephp code that works with the database to search a given card id number and return the balance. the jquery code looks like this.

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");
    },*/

});

I have the balancesController and balance.ctp files in place and controller logic looks like this.

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;
        }
    }
}

th problem that I have is - when a balance not available error is occurred "I AM GETTING THE CAKE DEBUG INFOMATION IN MY AJAX RESPONSE."

eg - when I try to access xhr object inside error function on $.ajax event
using "xhr.responseText" I am getting the long output consisting of
<pre class="cake-debug">.......... and at the end of this ONLY I get the error that I have encoded into json.
{"error":"error...."}
I have used Configure::write('debug', 1); and Configure::write('debug', 0); without any luck.as u can see I used Configure::write('debug', 0); in the top of my controller function as well..
please advice me resolve this issue. all your input is very highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-10-27 06:46:37

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

您正在使用 throw/catch。 Cake通常不使用异常来处理错误,除非您专门对模型进行了编码以引发异常,否则您的错误状态将不会被捕获。可能 $card 只是返回 false。

请将您的错误粘贴到此处,或者如果它很长,则粘贴到pastebin上。

If you're getting the debug message that means you have an error in your code and you should fix it instead of hiding it. Read the error message (or paste it here) to find out and fix the problem

You're using a throw/catch. Cake usually does not use exceptions to handle errors, unless you've specifically coded your model to throw exceptions, your error state won't be captured. Possibly $card is just returning false.

Please paste your error here, or on pastebin if it's really long.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文