问题描述
我有一个运行良好的 jqgrid.
I have a jqgrid that's functionning very well.
我想知道是否可以捕获服务器发送的错误?是怎么做的?
I was wondering is it possible to catch server sent errors ? how Is it done ?
推荐答案
我最近在我为 CB Richard Ellis(我的雇主)工作的原型项目中广泛使用了 jqgrid.如 文档 中所述,有很多方法可以填充 jqgrid:(请参阅检索数据"节点).
I have recently made extensive use of jqgrid for a prototype project I am working on for CB Richard Ellis (my employer). There are many way to populate a jqgrid, as noted in the documentation: (see the "retrieving data" node).
目前,我进行了一个服务调用,该调用返回一个 json 字符串,当评估该字符串时,会为我提供一个包含以下内容的对象:
Currently I make a service call that returns a json string that when evaluated, gives me an object that contains the following:
- 列名:字符串[]
- ColumnModels:object[](每个对象都有属性name"、index"和sortable")
- Data: object[](每个对象都有与列模型中的名称匹配的属性)
- TotalRows:整数
在我的成功回调中,我像这样手动创建 jqgrid:(数据"是我在评估返回的 json 字符串时得到的对象).
In my success callback, I manually create the jqgrid like this: ("data" is the object I get when evaluating the returned json string).
var colNames = data.ColumnNames;
var colModel = data.ColumnModels;
var previewData = data.PreviewData;
var totalRows = data.TotalRows;
var sTargetDiv = userContext[0]; // the target div where I'll create my jqgrid
$("#" + sTargetDiv).html("<table cellpadding='0' cellspacing='0'></table>");
var table = $("#" + sTargetDiv + " > table");
table.jqGrid({
datatype: 'local',
colNames: colNames,
colModel: colModel,
caption: 'Data Preview',
height: '100%',
width: 850,
shrinkToFit: false
});
for (var row = 0; row < previewData.length; ++row)
table.addRowData(row, previewData[row]);
所以你可以看到我手动填充数据.存在超过 1 种服务器错误.存在逻辑错误,您可以将其作为 json 字符串中的属性返回,并在尝试创建 jqgrid(或基于每行)之前检查.
So you can see I manually populate the data. There is more than 1 kind of server error. There is the logical error, which you could return as a property in your json string, and check before you try to create a jqgrid (or on a per-row basis).
if (data.HasError) ...
或以每行为基础
for (var row = 0; row < previewData.length; ++row)
{
if (previewData[row].HasError)
// Handle error, display error in row, etc
...
else
table.addRowData(row, previewData[row]);
}
如果您的错误是服务器上未处理的异常,那么您可能需要在异步调用中进行错误回调.在这种情况下,(大概)正在创建 jqgrid 的成功回调将不会被调用.
If your error is an unhandled exception on the server, then you'll probably want an error callback on your async call. In this case, your success callback that (presumably) is creating your jqgrid won't be called at all.
当然,这适用于手动填充 jqgrid,这只是众多可用选项之一.如果您将 jqgrid 直接连接到服务调用或函数以检索数据,那么这完全是另一回事.
This, of course, applies to manually populating a jqgrid, which is only one of the many options available. If you have the jqgrid wired directly to a service call or a function to retrieve the data, then that's something else entirely.
在文档页面上,查看基本网格 > 事件.在那里你会看到可能派上用场的loadError"事件.
On the documentation page, look under Basic Grids > Events. There you'll see the "loadError" event that might come in handy.
这篇关于如何让 JQGrid 识别服务器发送的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!