ajax 请求 jqgrid 后出现问题时显示漂亮的错误消息 [英] Display nice error message when there is something wrong after ajax request jqgrid

查看:20
本文介绍了ajax 请求 jqgrid 后出现问题时显示漂亮的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个函数删除行:

function deleteRow(){
 rows = jQuery("#category_grid").getGridParam('selarrrow');
 if( rows.length>0){
  jQuery('#category_grid').delGridRow(rows,{
   msg:'Verwijderen geselecteerde rijen?'   
  });
 }else{
  alert("Selecteer eerst een rij om te verwijderen!"); 
 }
}

但是当它在我的 php 中失败时,服务器端会抛出异常.错误消息看起来不太好.如何在对话框中显示错误文本?或者在 ajax 调用后捕获错误消息?

but when it's fails in my php, server side and a exception is thrown. The errormessage looks not nice. How can i show errotext in the dialog box? or catch an error message after an ajax call?

目前错误消息看起来像:错误状态:'CDbException'.错误代码:500

At the moment the error message looks like: error Status: 'CDbException'. Error code: 500

当我用谷歌搜索时,我发现了一个名为 errorTextFormat 的 delGridRow 函数的事件.这是我要找的活动吗?有人可以给我举个例子来说明这个事件的实施吗?

When i googled i found a event of the delGridRow function called errorTextFormat. Is this the event where i'm looking for? Can someone please give me an example of the implementation of this event?

问候

尼尔斯

推荐答案

delGridRow 的第二个参数是一个带选项的对象,所以你可以像下面这样操作

The second parameter of delGridRow is an object with options, so you can do like following

jQuery('#category_grid').delGridRow(rows,{
    errorTextFormat: function (data) {
        if (data.responseText.substr(0, 6) == "<html ") {
            return jQuery(data.responseText).html();
        }
        else {
            return data.responseText;
            // or
            // return "Status: '" + data.statusText + "'. Error code: " +data.status;
        }
    }
});

您通过errorTextFormat函数返回的文本retText将被放置在错误信息对应的div中,相对于jQuery.html(retText) delGridRow 函数内的代码.

The text retText, which you give back by errorTextFormat function will be placed in the corresponding div of the error message with respect of jQuery.html(retText) code inside of delGridRow function.

顺便说一下,我不直接调用 delGridRow 函数.如果我将导航栏添加到关于 navGrid 函数的 jqGrid,我将我的 errorTextFormat 函数作为标准删除按钮"的参数.确切地说,我这样做是为了 $.jgrid.del:

By the way I don't call delGridRow function directly. Instead of that if I add the navigation bar to the jqGrid with respect of navGrid function, I gives my errorTextFormat function as a parameter to standard "Delete button". To be exact I do this with respect of $.jgrid.del:

jQuery.extend(jQuery.jgrid.del, {
    ajaxDelOptions: { contentType: "application/json" },
    mtype: "DELETE",
    reloadAfterSubmit: false,
    jqModal: false,
    serializeDelData: function (postdata) {
        return "";
    },
    errorTextFormat: function (data) {
        if (data.responseText.substr(0, 6) == "<html ") {
            return jQuery(data.responseText).html();
        }
        else {
            return "Status: '" + data.statusText + "'. Error code: " + data.status;
        }
    }
});

(我的errorTextFormat的真实代码看起来有点复杂,但是使用思路是一样的).

(the real code of my errorTextFormat looks like a little more complex, but the idea of usage is the same).

这篇关于ajax 请求 jqgrid 后出现问题时显示漂亮的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆