$.post()
sends an asynchronous request. If you reload the page before that request is complete, the request will be aborted.
In your case, you are sending out n requests all at once in the for loop, and then immediately reloading the page (with this line window.location.reload();
) before any of them have time to complete. To solve this, you can either consolidate them all into one $.post
request and use the success callback, or you can store each promise object returned from $.post()
in an array and pass it to $.when
.
I suggest using the first solution of consolidating all of the requests into one request and using the success callback, however that will require you to either modify your current cfc method to accept multiple records to delete at once, or to create a new cfc method that can handle it.
One way would be to have your method capable of handling a list of id's rather than a single id.
<cffunction name="deleteRateCat" access="remote" returntype="void" output="no" hint="Delete Rating Categories.">
<cfargument name="recID" type="string" required="true" hint="The id of the rating category to delete.">
<cfquery datasource="#dsn#">
delete from rating_categories
where id in (<cfqueryparam cfsqltype="cf_sql_integer" value="#ListAppend(arguments.recID, 0)#" list="yes">)
</cfquery>
</cffunction>
And the js to go with it:
function dlteCatR(field){
var r = confirm("Are you sure you want to delete this Category? \n You will not be able to revert this change!")
if(r==true){
var recIdList = $("[name=" + field + "]:checked").map(function(){
return this.value;
}).get().join(",");
var url="surveyAdmin.cfc?wsdl&method=deleteRateCat&recId="+recIdList;
$.post(url).done(function(){
window.location.reload();
});
}
}