Coldfusion:将 json 从一个 CFM 传递到另一个 CFM?

发布于 2024-12-26 21:28:33 字数 991 浏览 5 评论 0原文

基本上,我从一个 CFM 调用另一个 CFM,它创建一个对象,调用该对象上的多个方法并登录用户;否则它会在屏幕上打印错误。

有没有办法获取 CFM 并将 JSON 对象发送到调用它的文件,而不是打印错误?

这是我的阿贾克斯:

// processing the login form using jQuery
    $("#loginForm").submit(function(event){
        // prevents the form from being submitted, the event is the arg to the function
        event.preventDefault();

        // stores the data from the form into a variable to be used later
        dataString = $("#loginForm").serialize();

        // the AJAX request 
        $.ajax
        ({
            type: "POST",
            url: "/helpers/auth/ldap/login_demo.cfm",
            data: dataString,
            //dataType: "text",
            success: function() 
            {
                location.reload();
            },
            error: function(ErrorMsg) 
            {
                $("#hiddenLoginError").show("fast"); 
                $("#loginForm p").css("margin-bottom","3px");
            }
        });

    });

Basically I am calling from a CFM another CFM which creates an object, calls several methods on this object and logs a user in; otherwise it prints an error on the screen.

Rather then printing the Error, is there a way to take a CFM and have it send a JSON object to the file that called it?

Here's my ajax:

// processing the login form using jQuery
    $("#loginForm").submit(function(event){
        // prevents the form from being submitted, the event is the arg to the function
        event.preventDefault();

        // stores the data from the form into a variable to be used later
        dataString = $("#loginForm").serialize();

        // the AJAX request 
        $.ajax
        ({
            type: "POST",
            url: "/helpers/auth/ldap/login_demo.cfm",
            data: dataString,
            //dataType: "text",
            success: function() 
            {
                location.reload();
            },
            error: function(ErrorMsg) 
            {
                $("#hiddenLoginError").show("fast"); 
                $("#loginForm p").css("margin-bottom","3px");
            }
        });

    });

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

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

发布评论

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

评论(2

小猫一只 2025-01-02 21:28:33

在您的 CFM 页面上,您可以使用 语句来检查该页面是否是通过 ajax 请求的。如果是ajax,则返回一个json对象。

<cfset isAjax = (isDefined('cgi.http_x_requested_with') AND lcase(cgi.http_x_requested_with) EQ 'xmlhttprequest')>
...
<cfif isAjax>
  <cfcontent reset="true">{ "result":false, "message": "Login Failure" }<cfabort>
</cfif>

然后你可以测试响应:

dataType: "json",
success: function(rdata) {
  alert(rdata.message);
}

On your CFM page, you can have a <cfif> statement that checks whether the page was requested with ajax or not. If it was ajax, return a json object.

<cfset isAjax = (isDefined('cgi.http_x_requested_with') AND lcase(cgi.http_x_requested_with) EQ 'xmlhttprequest')>
...
<cfif isAjax>
  <cfcontent reset="true">{ "result":false, "message": "Login Failure" }<cfabort>
</cfif>

you can then test the response:

dataType: "json",
success: function(rdata) {
  alert(rdata.message);
}
千年*琉璃梦 2025-01-02 21:28:33

尝试:

<cfoutput>#serializeJSON( object )#</cfoutput>

Try:

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