删除记录时未找到任何元素 [英] No element found when delete a record

查看:96
本文介绍了删除记录时未找到任何元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JqGrid 4.6。



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


未找到任何元素


可能的脚本是:

  $(gridSelector).jqGrid('navGrid',pagerSelector,
{
//导航栏选项
编辑: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',
搜索:true,
searchicon:'ace-icon fa fa-search orange',
refresh:true,
refreshicon:'ace-icon fa fa-refresh green',
view:true,
viewicon:'ace-icon fa fa-searc h-plus gray',
beforeRefresh:function(){
grid.jqGrid('setGridParam',{datatype:'json'})。trigger('reloadGrid');
}
},

{
//删除记录表
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);
返回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中的错误?

解决方案

创建



我完全检查了问题并且它确实是错误警告,因为错误解释了REST操作的HTTP流量。 ASP.NET MVC的DELETE方法,其中 void 作为返回值(如 public void DeleteProduct(int id) )生成HTTP响应,如

  HTTP / 1.1 204无内容
缓存控制:无缓存
Pragma:no-cache
到期:-1
服务器:Microsoft-IIS / 10.0
X-AspNet-版本:4.0.30319
X-SourceFiles:=?UTF- 8?B?QzpcVXNlcnNcT2xlZ1xEb3dubG9hZHNcUHJvZHVjdFN0b3JlXFByb2R1Y3RTdG9yZVxhcGlccHJvZHVjdHNcNA ==?=
X-Powered-By:ASP.NET
日期:星期五,2016年2月12日09:23:51 GMT

Firefox的错误:它显示所有没有正文的HTTP响应的消息找不到元素。因此,如果状态代码是 204 (无内容),或者状态代码是 200 (OK),但是body是空的(存在HTTP标头 Content-Length:0 )然后Firefox怀疑找不到REST资源,它显示警告,文本为no找到元素。



如果您不想看到该消息,则必须在DELETE响应正文中返回一些数据。例如

  public HttpResponseMessage DeleteProduct(int id)
{
bool isDeleted = _repository.Remove(id );
if(!isDeleted){
抛出新的HttpResponseException(HttpStatusCode.NotFound);
}
返回Request.CreateResponse(HttpStatusCode.OK,OK!);
}

产生响应,如

  HTTP / 1.1 200 OK 
Cache-Control:no-cache
Pragma:no-cache
Content-Type:application / json; charset = utf-8
到期:-1
服务器:Microsoft-IIS / 10.0
X-AspNet-版本:4.0.30319
X-SourceFiles:=?UTF-8 ?B?QzpcVXNlcnNcT2xlZ1xEb3dubG9hZHNcUHJvZHVjdFN0b3JlXFByb2R1Y3RTdG9yZVxhcGlccHJvZHVjdHNcNg ==?=
X-Powered-By:ASP.NET
日期:星期五,2016年2月12日09:05:19 GMT
内容长度:5

好的!

我个人认为应该更好地忽略Firefox的警告并持有 public HttpResponseMessage DeleteProduct(int id)。我仍然建议你更新你用来的存储库

  interface IProductRepository 
{
IEnumerable<产品与GT;得到所有();
Product Get(int id);
产品添加(产品项目);
bool Remove(int id);
bool更新(产品项目);
}

其中删除有布尔值作为返回类型。实现可以是

  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)
{
抛出新的HttpResponseException(HttpStatusCode.NotFound);
}
}

我想强调以上所有问题都是纯ASP .NET MVC问题或Firefox的问题,它与免费的jqGrid或jqGrid没有直接关系。



你可以下载修改后的项目这里 ProductsController.cs 文件包含注释版本的 DeleteProduct ,它不会在Firefox中产生任何警告。您可以通过将虚拟文本OK!更改为空字符串或其他一些代码来播放代码试验。 Firefox的bug很老(它的原始接口是 Bug 521301 )。


JqGrid 4.6.

Everything works fine. The only thing is that when I open the Firefox debugger and go to the console. If I delete a record(click the trash icon, then the delete dialog pops out, click Delete button and the page is refreshed etc), the debugger warns me.

no element found

The possible scripts are:

$(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;
                }
            }

Also

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

Although the error has not impacted my result. I can't locate the warning. I want remove it.

EDIT:

I tried it in google chrome. It seems okay. Maybe it is the bug in Firefox?

解决方案

After creating the demo project which can be used to reproduce "the problem" I can examine and describe it.

To reproduce the problem one need to start the MVC application and to use Firefox as frontend. One should start the integrated Debugger (by Ctrl+Shift+S or menu "Tools" / "Web Developer" / "Debugger") and to examine Browser Console window. The window contains many warnings, which are suspected for Firefox, but what are absolutely correct actions and the warnings are absolutely unneeded. After deleting of any row one will see the message like

I examined the problem exactly and it's really wrong warning, because of misinterpretation of HTTP traffic of REST operation. The DELETE method of ASP.NET MVC, which have void as return value (like public void DeleteProduct(int id)) produces the HTTP response like

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

The bug of Firefox: it displays message "no element found" for all HTTP responses which have no body. Thus if the status code is 204 (No Content) or if the status code is 200 (OK), but the body is empty (there are exist HTTP header Content-Length: 0) then Firefox suspect that the REST resource is not found and it display the "warning" with the text "no element found".

If you don't want to see the message then you have to return some data in the body of DELETE response. For example

public HttpResponseMessage DeleteProduct(int id)
{
    bool isDeleted = _repository.Remove(id);
    if (!isDeleted) {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
    return Request.CreateResponse(HttpStatusCode.OK, "OK!");
}

which produces the response like

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!"

I personally think that one should better just ignore "the warning" of Firefox and to hold public HttpResponseMessage DeleteProduct(int id). I would still recommend you to update the repository which you use to

interface IProductRepository
{
    IEnumerable<Product> GetAll();
    Product Get(int id);
    Product Add(Product item);
    bool Remove(int id);
    bool Update(Product item);
}

where Remove have Boolean as return type. The implementation can be

public bool Remove(int id)
{
    return _products.RemoveAll(p => p.Id == id) > 0;
}

and the MVC code

public void DeleteProduct(int id)
{
    _repository.Remove(id);
}

will be fixed to

public void DeleteProduct(int id)
{
    bool isDeleted = _repository.Remove(id);
    if (!isDeleted)
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
}

I want to stress that all above problem is pure ASP.NET MVC problem or a problem of Firefox and it have no direct relation to free jqGrid or jqGrid.

You can download the modified project here. The ProductsController.cs file contains commented version of DeleteProduct, which don't produce any warning in Firefox. You can play with the code by changing the dummy text "OK!" to the empty string "" or some other tests. The Firefox bug is very old (it's origin seams be the Bug 521301).

这篇关于删除记录时未找到任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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