如何使用 jQuery.post() 将 JSON 数据返回到 Coldfusion 8 cfc?

发布于 2024-08-16 23:16:30 字数 2276 浏览 7 评论 0原文

如何使用 jQuery.post() 将表单发布到 Coldfusion.cfc 方法并返回 json 数据?我是否需要通过某种方式格式化 url 或表单值才能指定要远程调用的 cfc 方法?如何告诉 Coldfusion 返回 json 数据?

我搜索了现有的 jQuery/Coldfusion.cfc 问题,希望得到一些澄清。我找不到显示与 Coldfusion cfc 之间的完整流程的示例。

HTML 表单:

<!--- Assume: jquery, jquery-ui, sample.js is loaded --->
<p><a href="#" id="openDialog">Open Dialog</a></p>

<div id="myDialog" title="My Dialog" class="dialog">
<form id="myForm">
    <label for="title">Title:</label><br />
    <input type="text" name="title" id="title" value="" /><br />
    <label for="address">Address:</label><br />
    <input type="text" name="address" id="address" value="" /><br />
    <input type="submit" name="save" value="save" />
</form>
</div>

sample.js:

jQuery(function ($) {

    var saveUrl = "remote.cfc?method=save";  // Is this the right way??

    // Bind link to open the dialog box
    $('a#openDialog').click(function() {
        $('#myDialog').dialog('open');
    });

    // Configure jQuery UI dialog box and callback methods
    // Is this right??
    $("#myForm").dialog({
        buttons: {
            'Save': function() { 
                $.post(saveUrl, $("#myForm").serialize(), function(result){
                    alert("Result: " + result);
                    }, "json");
                $(this).dialog('close');
               },
            'Cancel': function() {
                $(this).dialog('close');
            }
    });
});

remote.cfc:

<cfcomponent>

    <!--- If I set the returnFormat to JSON do I need to specify json in the post too? --->
    <cffunction name="save" access="remote" returntype="string" returnFormat="JSON">
        <cfargument name="title" type="string" required="yes">
        <cfargument name="address" type="string" required="yes">
        <cfset var result = structNew()>

        <!--- Do some data manipulation or cfquery here, save to result struct --->

        <cfreturn result>
    </cffunction>

</cfcomponent>

*注意,我发现使用 Coldfusion 调试确实会弄乱 cfc 返回值,因此应该抑制或关闭它。

How do I post a form using jQuery.post() to a Coldfusion.cfc method and return json data? Is there a certain way I need to format the url or form values in order to specify the cfc method to invoke remotely? How do I tell Coldfusion to return json data?

I've searched the existing jQuery/Coldfusion.cfc questions and I'm looking for some clarity. I can't find an example that shows the full process to/from a Coldfusion cfc.

HTML Form:

<!--- Assume: jquery, jquery-ui, sample.js is loaded --->
<p><a href="#" id="openDialog">Open Dialog</a></p>

<div id="myDialog" title="My Dialog" class="dialog">
<form id="myForm">
    <label for="title">Title:</label><br />
    <input type="text" name="title" id="title" value="" /><br />
    <label for="address">Address:</label><br />
    <input type="text" name="address" id="address" value="" /><br />
    <input type="submit" name="save" value="save" />
</form>
</div>

sample.js:

jQuery(function ($) {

    var saveUrl = "remote.cfc?method=save";  // Is this the right way??

    // Bind link to open the dialog box
    $('a#openDialog').click(function() {
        $('#myDialog').dialog('open');
    });

    // Configure jQuery UI dialog box and callback methods
    // Is this right??
    $("#myForm").dialog({
        buttons: {
            'Save': function() { 
                $.post(saveUrl, $("#myForm").serialize(), function(result){
                    alert("Result: " + result);
                    }, "json");
                $(this).dialog('close');
               },
            'Cancel': function() {
                $(this).dialog('close');
            }
    });
});

remote.cfc:

<cfcomponent>

    <!--- If I set the returnFormat to JSON do I need to specify json in the post too? --->
    <cffunction name="save" access="remote" returntype="string" returnFormat="JSON">
        <cfargument name="title" type="string" required="yes">
        <cfargument name="address" type="string" required="yes">
        <cfset var result = structNew()>

        <!--- Do some data manipulation or cfquery here, save to result struct --->

        <cfreturn result>
    </cffunction>

</cfcomponent>

*Note, I've discovered that having Coldfusion debugging will really goof up cfc return values, so it should be suppressed or turned off.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨笨の傻瓜 2024-08-23 23:16:30

你的看起来不错,哪里出错了,错误是什么?如果没有明显的错误消息,我要做的第一件事就是在远程方法中抛出一条日志语句,看看您的调用是否正在发送到服务器。如果这是真的并且它一直到最后,那么警告返回回调的对象(看起来你已经这样做了)。

要回答您的具体问题,remote.fc?method=methodName 是正确的 URL,如果您要发布数据,则应该有标题等,所以应该没问题。如果您收到错误或最终警报的值是多少,请发回错误。

What you have looks good, where does it hit an error and what is the error? If there's no obvious error message, the first thing I would do is throw a log statement in the remote method and see if you're call is making it to the server. If that's true and it makes it all the way to the end, then alert the object that comes back to callback (looks like you're already doing that).

To ansdwer your specific question, remote.fc?method=methodName is the correct URL and if you're posting data, that should have the title, etc. so you should be fine. Post back an error if you're receiving one or what the value of your final alert is.

染年凉城似染瑾 2024-08-23 23:16:30

如果将 returnFormat 设置为 JSON,则无需在帖子中指定 json。出于向后兼容性的原因,默认情况下 returnformat=WDDX。

如果您想要易于使用,请查看 和各种 cf-ajax UI 组件标签。

查看此相关问题:使用 AJAX 调用 ColdFusion 函数

If you set the returnFormat to JSON you don't need to specify json in the post. returnformat=WDDX by default for backward compatibility reason.

If you want ease of use, check out <cfajaxproxy> and various cf-ajax UI-component tags.

Check out this related Question: Invoke ColdFusion function using AJAX

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文