`当我调用 api 并删除超过 1 个建筑物时,它不起作用。但是当我删除 1 栋建筑时,就可以了。我认为我的 json 错误,请帮我修复它
我尝试搜索,但没有看到任何想法。
这是我的 json 代码: $('#btnDeleteBuilding').click(函数 (e) {
e.preventDefault(); var buildingIds=$('#tableList').find('tbody input[type=checkbox]:checked').map(function(){ return $(this).val(); }).get(); deleteBuildings(buildingIds); });
函数deleteBuildings(buildingID) {
变量数据 = {};
如果(buildingID == null){ buildingID = $("#listForm").find('tbody input[type=checkbox]:checked').map(function () { return $(this).val(); }).get().join(",");
}
数据['id'] = 建筑物ID;
$.ajax({ url: "${buildingAPI}", type: "DELETE",
数据:JSON.stringify(数据),
dataType: "json",contentType: "application/json", success: function (response) {
console.log("成功")
},
错误:函数(响应){
console.log("失败");
控制台.log(响应)``
},`
});
}`
这是我的函数删除:`
@Transactional
public void deleteBuilding(Long[] id) {`
for(Long item:id){
List<RentAreaEntity> list=rentAreaRepository.findByBuildingId(item);`
for(RentAreaEntity item1:list){
rentAreaRepository.deleteById(item1.getId());
}
` List<AssignmentBuildingEntity> list1=as`signmentBuildingRepository.findAll();
list1.forEach(it->{if (it.getId().equals(id)){ assignmentBuildingRepository.deleteById(it.getId());
}
});
` buildingRepository.deleteById(item);``
}
}
My Entity:
`
@Entity
@Table(name="rentarea")
public class RentAreaEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name="buildingid")
private BuildingEntity building;
@Column(name="value")
private Long value;
public BuildingEntity getBuilding() {
return building;
}
public void setBuilding(BuildingEntity building) {
this.building = building;
}
@Column(name="createddate")
private Date createddate;
@Column(name="modifieddate")
private Date modifieddate;
@Column(name="createdby")
private String createdby;
public Date getCreateddate() {
return createddate;
}
@Column(name="modifiedby")
private String modifiedby;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getValue() {
return value;
}
public void setValue(Long value) {
this.value = value;
}
public Date createddate() {
return createddate;
}
public void setCreateddate(Date createddate) {
this.createddate = createddate;
}
public Date getModifieddate() {
return modifieddate;
}
public void setModifieddate(Date modifieddate) {
this.modifieddate = modifieddate;
}
public String getCreatedby() {
return createdby;
}
public void setCreatedby(String createdby) {
this.createdby = createdby;
}
public String getModifiedby() {
return modifiedby;
}
public void setModifiedby(String modifiedby) {
this.modifiedby = modifiedby;
}
}
`
My json:
function deleteBuildings(buildingID) {
var data = {};
if(buildingID == null){
buildingID = $("#listForm").find('tbody input[type=checkbox]:checked').map(function () {
return $(this).val();
}).get().join(",");
}
data['id'] = buildingID;
$.ajax({
url: "${buildingAPI}",
type: "DELETE",
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json",
success: function (response) {
console.log("success")
},
error: function (response) {
console.log("fail");
console.log(response)
},
});
window.location.href = "<c:url value='/admin/building-list?message-success'/>";
}