【问题标题】:How to make arraList empty if the ajax response is error?如果ajax响应错误,如何使arrayList为空?
【发布时间】:2019-06-23 09:11:42
【问题描述】:

我初始化了一个空的arrayList,它是 var selection11Data = [];。我从 AJAX 调用中获取数据:

var selectionId=trData.selectionId;
            console.log("here");
            $.ajax({
                url : A_PAGE_CONTEXT_PATH + "/form/api/getSelectionAuditGroup/" + selectionId,
                method : "GET",
                dataType : "json",
                success : function(response) {
                    console.log("response here is");
                    console.log(response);
                    if(response.msg== "error"){
                        selection11Data =[];
                        $('#selectionWithAuditorGroupTable').hide();
                        alert("Selection Id"+selectionId+"is not assigned to any group");
                    }
                    else{
                        selection11Data = response;
                        $('#selectionWithAuditorGroupTable').show();
                        selectionWithAuditorGroup.rows.add(selection11Data).draw();
                    }

                }
            });

如果响应正在获取数据,则将其填充到 dataTable 中。但是,如果响应“msg”是错误的,则数据表被隐藏,在这种情况下,我隐藏了我的 dataTable 并想清空 arraylist @987654322 @。但是,它并没有变空,当我得到新数据时,arrayList 仍然包含旧数据加上新的响应数据。在这种情况下,我只需要新的响应数据。当我试图清除 arrayList 时,如何清空这个 arrayList如果响应错误:

selection11Data =[];

【问题讨论】:

  • 你能举例说明清楚吗?

标签: javascript jquery html ajax datatable


【解决方案1】:

你可以这样试试:

    var selectionId=trData.selectionId;
                console.log("here");
                $.ajax({
                    url : A_PAGE_CONTEXT_PATH + "/form/api/getSelectionAuditGroup/" + selectionId,
                    method : "GET",
                    dataType : "json",
                    success : function(response) {
                        console.log("response here is");
                        console.log(response);
                        if(response.msg== "error"){
                            selection11Data =[];
                            $('#selectionWithAuditorGroupTable').hide();
                            alert("Selection Id"+selectionId+"is not assigned to any group");
                        }
                        else{
                            selection11Data = response;
                            $('#selectionWithAuditorGroupTable').show();
                            selectionWithAuditorGroup.rows.add(selection11Data).draw();
                  //arraylist gets empty now
                           selection11Data =[];
                        }

                    }
                });

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多