Display nice error message when there is something wrong after ajax request jqgrid

淺唱寂寞╮ 提交于 2020-01-09 11:28:14

问题


I delete rows with this function:

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!"); 
 }
}

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?

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

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?

greetings

niels


回答1:


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

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.

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

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



来源:https://stackoverflow.com/questions/2849189/display-nice-error-message-when-there-is-something-wrong-after-ajax-request-jqgr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!