删除记录时找不到任何元素
JqGrid 4.6。
一切正常。 唯一的事情是当我打开Firefox调试器并转到控制台时。 如果我删除一条记录(单击垃圾桶图标,然后弹出删除对话框,单击删除按钮,刷新页面等),调试器会发出警告。
找不到任何元素
可能的脚本是:
$(gridSelector).jqGrid('navGrid', pagerSelector, { //navbar options edit: true, editicon: 'ace-icon fa fa-pencil blue', add: true, addicon: 'ace-icon fa fa-plus-circle purple', del: true, delicon: 'ace-icon fa fa-trash-o red', search: true, searchicon: 'ace-icon fa fa-search orange', refresh: true, refreshicon: 'ace-icon fa fa-refresh green', view: true, viewicon: 'ace-icon fa fa-search-plus grey', beforeRefresh: function () { grid.jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid'); } }, { //delete record form closeAfterDelete: true, recreateForm: true, mtype: 'DELETE', onclickSubmit: function (params, postdata) { params.url = API_URL + 'DeleteVendor'; }, beforeShowForm: function (e) { var form = $(e[0]); if (form.data('styled')) return false; form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner(''); styleDeleteForm(form); form.data('styled', true); return true; } }
也
function styleDeleteForm(form) { var buttons = form.next().find('.EditButton .fm-button'); buttons.addClass('btn btn-sm btn-white btn-round').find('[class*="-icon"]').hide(); //ui-icon, s-icon buttons.eq(0).addClass('btn-danger').prepend(''); buttons.eq(1).addClass('btn-default').prepend(''); }
虽然错误没有影响我的结果。 我无法找到警告。 我想删除它。
编辑:
我在谷歌浏览器中尝试过它。 好像没关系。 也许这是Firefox中的错误?
创建可用于重现“问题” 的演示项目后 ,我可以检查和描述它。
要重现问题,需要启动MVC应用程序并使用Firefox作为前端。 应该启动集成调试器(通过Ctrl + Shift + S或菜单“工具”/“Web Developer”/“调试器”)并检查浏览器控制台窗口。 该窗口包含许多警告,怀疑是Firefox,但绝对正确的操作和警告是绝对不需要的。 删除任何一行后,会看到如下消息
我确切地检查了这个问题并且这是非常错误的警告 ,因为错误解释了REST操作的HTTP流量。 ASP.NET MVC的DELETE方法,其返回值为public void DeleteProduct(int id)
(如public void DeleteProduct(int id)
),产生HTTP响应,如
HTTP/1.1 204 No Content Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcT2xlZ1xEb3dubG9hZHNcUHJvZHVjdFN0b3JlXFByb2R1Y3RTdG9yZVxhcGlccHJvZHVjdHNcNA==?= X-Powered-By: ASP.NET Date: Fri, 12 Feb 2016 09:23:51 GMT
Firefox的错误: 它显示所有没有正文的HTTP响应的消息“找不到元素” 。 因此,如果状态代码为204
(无内容)或状态代码为200
(OK),但正文为空(存在HTTP标头Content-Length: 0
),则Firefox怀疑未找到REST资源,它显示“警告”,文本“找不到元素”。
如果您不想看到该消息,则必须在DELETE响应正文中返回一些数据。 例如
public HttpResponseMessage DeleteProduct(int id) { bool isDeleted = _repository.Remove(id); if (!isDeleted) { throw new HttpResponseException(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK, "OK!"); }
产生响应就像
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcT2xlZ1xEb3dubG9hZHNcUHJvZHVjdFN0b3JlXFByb2R1Y3RTdG9yZVxhcGlccHJvZHVjdHNcNg==?= X-Powered-By: ASP.NET Date: Fri, 12 Feb 2016 09:05:19 GMT Content-Length: 5 "OK!"
我个人认为最好不要忽略Firefox的“警告”并保留public HttpResponseMessage DeleteProduct(int id)
。 我仍然建议您更新您使用的存储库
interface IProductRepository { IEnumerable GetAll(); Product Get(int id); Product Add(Product item); bool Remove(int id); bool Update(Product item); }
其中Remove
使用Boolean作为返回类型。 实施可以
public bool Remove(int id) { return _products.RemoveAll(p => p.Id == id) > 0; }
和MVC代码
public void DeleteProduct(int id) { _repository.Remove(id); }
将被固定
public void DeleteProduct(int id) { bool isDeleted = _repository.Remove(id); if (!isDeleted) { throw new HttpResponseException(HttpStatusCode.NotFound); } }
我想强调的是,以上所有问题都是纯ASP.NET MVC问题或Firefox的问题,它与免费的jqGrid或jqGrid没有直接关系。
您可以在此处下载修改后的项目。 ProductsController.cs
文件包含注释版本的DeleteProduct
,它不会在Firefox中产生任何警告。 您可以通过更改虚拟文本"OK!"
来播放代码"OK!"
到空字符串""
或其他一些测试。 Firefox的bug很老(它的原始接缝是Bug 521301 )。