Why is jQuery not getting the returned json info from my ColdFusion CFC in this code?
I have a CFC that cycles through a folder and deletes any files in said folder, sort of a 'clean-up' function after an image is uploaded and saved. In that same CFC I have a function that updates text in a database. Bot functions are fired through a jQuery post. The text function returns some a confirmation to my jQuery function, no problem.
But, the clean-up function isn't returning data to my page. Can anyone see an obvious error in my coding that would keep the clean-up function from returning data to my page and firing the confirmation?
I know the CFC is working because the files are deleted from th开发者_Go百科e folder but it's simply not returning a 'valid' response.
Here's the jQuery:
function rebinder(deleteImages){
$('.editingFormField').attr('value', '');
if ($('.edit').hasClass('selected')){
$('.edit').removeClass('selected');
}
$('#imagePreview').attr('src', '');
$('#et').dialog('close');
$('#ei').dialog('close');
if (deleteImages == 'yes'){
$.post("cfc/engine.cfc?method=clearImages&returnformat=json",
{},
function(ret) {
//Handle the result
alert(ret + "the Return");
if(ret == "true") {
} else {
alert("There was an error in the processing (files_no_del)");
}
});
$.post("cfc/engine.cfc?method=clearThumbs&returnformat=json",
{},
function(ret2) {
//Handle the result
if(ret2 == "true") {
} else {
alert("There was an error in the processing (thumbs_no_del)");
}
});
}
location.reload();
};
And the CFC:
<cffunction name="clearImages" access="remote" output="false" returntype="boolean">
<cfset var deleteConfirm = "true">
<!--- Read Holding Directory --->
<cfdirectory
action="list"
directory="#destdir#"
recurse="true"
listinfo="name"
name="qFile"
/>
<!--- Loop through file query and delete files --->
<cfloop query="qFile">
<cffile action="delete" file="#destdir#/#qFile.name#">
</cfloop>
<cfreturn deleteConfirm>
</cffunction>
The code is fully functional except for the placement of location.reload();
Once I moved the reload command into the section of my code that fires when it hears back from the CFC, it worked fine. Nick Craver covered the problem in this post: jQuery window reload doesn't allow to work post.
It's worth mentioning that Tony's and Ken's comments also helped in diagnosing the problem. Running the post URL did confirm that the AJAX magic was happening as planned.
Here's the revised code that works:
function rebinder(deleteImages){
$('.editingFormField').attr('value', '');
if ($('.edit').hasClass('selected')){
$('.edit').removeClass('selected');
}
$('#et').dialog('close');
$('#ei').dialog('close');
$('#imagePreview').attr('src', '');
if (deleteImages == 'yes'){
$.post("cfc/engine.cfc?method=clearImages&returnformat=json",
{},
function(ret) {
//Handle the result
if(ret == "true") {
location.reload();
} else {
alert("There was an error in the processing (files_no_del)");
}
});
} else {
location.reload();
}
};
The only real change I made was to consolidate my CFC a little. Instead of running two methods to clean out two folders I've boiled it down to one 'clean-up' method that empties both folders.
I have just sneaked through your code, can you use $.getJSON() function to make a call to CFC, you can mention the method that you want to invoke and optionally pass arguments to the method.
$.getJSON('Some.cfc', {method: 'fetchImapStartMessages', arg1: 'Hello',
returnformat: 'json' },
function(returndata) {
//some code here that renders the data
}
});
and make sure you set attributes returnformat = "JSON" access="remote" in your CFC method. when the data is returned from CFC method make sure you use serializeJSON(returndata). Does this help?
精彩评论