为什么jQuery在这段代码中没有从我的ColdFusion CFC获得返回的json信息?

时间:2022-10-24 22:43:59

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.

我有一个循环文件​​夹的CFC并删除所述文件夹中的所有文件,在上传和保存图像后进行“清理”功能。在同一个CFC中,我有一个更新数据库中文本的函数。 Bot函数通过jQuery帖子触发。 text函数返回一些确认给我的jQuery函数,没问题。

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 the folder but it's simply not returning a 'valid' response.

我知道CFC正在运行,因为文件已从文件夹中删除,但它根本没有返回“有效”响应。

Here's the jQuery:

这是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:

和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>

2 个解决方案

#1


1  

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.

代码完全正常,除了location.reload();一旦我将reload命令移动到我的代码部分,当它从CFC回来时触发,它运行正常。 Nick Craver在这篇文章中讨论了这个问题:jQuery窗口重新加载不允许工作。

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.

值得一提的是Tony和Ken的评论也有助于诊断问题。运行帖子URL确实证实了AJAX魔法按计划发生。

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.

我做的唯一真正的改变是巩固我的CFC。我没有运行两种方法来清理两个文件夹,而是将其归结为一个清理两个文件夹的“清理”方法。

#2


0  

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()函数来调用CFC,你可以提到你想要调用的方法,也可以选择将参数传递给方法。

$.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?

并确保在CFC方法中设置属性returnformat =“JSON”access =“remote”。当从CFC方法返回数据时,请确保使用serializeJSON(returndata)。这有帮助吗?

#1


1  

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.

代码完全正常,除了location.reload();一旦我将reload命令移动到我的代码部分,当它从CFC回来时触发,它运行正常。 Nick Craver在这篇文章中讨论了这个问题:jQuery窗口重新加载不允许工作。

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.

值得一提的是Tony和Ken的评论也有助于诊断问题。运行帖子URL确实证实了AJAX魔法按计划发生。

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.

我做的唯一真正的改变是巩固我的CFC。我没有运行两种方法来清理两个文件夹,而是将其归结为一个清理两个文件夹的“清理”方法。

#2


0  

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()函数来调用CFC,你可以提到你想要调用的方法,也可以选择将参数传递给方法。

$.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?

并确保在CFC方法中设置属性returnformat =“JSON”access =“remote”。当从CFC方法返回数据时,请确保使用serializeJSON(returndata)。这有帮助吗?