Javascript 如果ajax响应出错,如何使arraList为空?
我初始化了一个空的arrayList,它是var selection11Data=[]。我从AJAX调用中获取的数据如下:Javascript 如果ajax响应出错,如何使arraList为空?,javascript,jquery,html,ajax,datatable,Javascript,Jquery,Html,Ajax,Datatable,我初始化了一个空的arrayList,它是var selection11Data=[]。我从AJAX调用中获取的数据如下: var selectionId=trData.selectionId; console.log("here"); $.ajax({ url : A_PAGE_CONTEXT_PATH + "/form/api/getSelectionAuditGroup/" + selectionId,
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,并希望清空arraylistselection11Data
。但是,它不是空的,当我获得新数据时,arrayList仍然包含旧数据和新响应数据。在这种情况下,我只需要新响应数据。如何在响应出错时清空此arrayList,因为我尝试清除arrayList:
selection11Data =[];
您可以这样尝试:
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 =[];
}
}
});
你能举例说明清楚吗?