javascript - 删除记录时找不到元素

标签 javascript jquery asp.net-mvc firefox jqgrid

JqGrid 4.6。

一切正常。唯一的问题是当我打开 Firefox 调试器并转到控制台时。如果我删除一条记录(点击垃圾桶图标,弹出删除对话框,点击删除按钮,页面刷新等),调试器会警告我。

no element found

可能的脚本是:

$(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('<div class="widget-header" />');
                    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('<i class="ace-icon fa fa-trash-o"></i>');
            buttons.eq(1).addClass('btn-default').prepend('<i class="ace-icon fa fa-times"></i>');
        }

虽然这个错误并没有影响我的结果。我找不到警告。我想删除它。

编辑:

我在谷歌浏览器中试过了。好像还可以也许这是 Firefox 中的错误?

最佳答案

创建后the demo project可以用来重现“问题”我可以检查和描述它。

要重现问题,需要启动 MVC 应用程序并使用 Firefox 作为前端。应该启动集成调试器(通过 Ctrl+Shift+S 或菜单“工具”/“Web 开发人员”/“调试器”)并检查浏览器控制台窗口。该窗口包含许多警告,这些警告是 Firefox 怀疑的,但这些操作是绝对正确的,而且这些警告是绝对不需要的。删除任何一行后,将看到如下消息

enter image description here

我仔细检查了这个问题,它确实是错误警告,因为对 REST 操作的 HTTP 流量的误解。 ASP.NET MVC 的 DELETE 方法以 void 作为返回值(如 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 header 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<Product> 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 没有直接关系。

您可以下载修改后的项目here . ProductsController.cs 文件包含注释版本的 DeleteProduct,它不会在 Firefox 中产生任何警告。您可以通过将虚拟文本 "OK!" 更改为空字符串 "" 或进行一些其他测试来使用代码。 Firefox 错误非常古老(它的起源接缝是 the Bug 521301 )。

关于javascript - 删除记录时找不到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35300375/

相关文章:

javascript - 使用 .load() 为简单的 slider1.7 用图像填充 div

javascript - 自动滚动到 anchor 时的平滑效果

sql-server - 博客应用程序的最佳数据库(关系型 - SQL 与 NoSQL)

ASP.NET MVC 安全和 IIS allowSubDirConfig 配置

javascript - Angular 中 2 个导入之间的差异

JavaScript 页面加载问题

javascript - ESLint 定义前不使用

javascript - 在 Controller 中使用 Angular 处理浏览器后退按钮

asp.net - ASP MVC 4 返回 json 复杂对象

按下按钮时,Javascript简单类作用域会发生变化