Ajax jQuery调用ColdFusion组件 [英] Ajax jQuery call to ColdFusion component

查看:90
本文介绍了Ajax jQuery调用ColdFusion组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手,我正在尝试创建一个登录页面,该页面将对CFC执行Ajax调用,只需返回true或false,以确定登录是否成功。我的调用是正确地使用我的参数进入CFC,但返回的是问题。如果我将jQuery中的数据类型设置为html,我会在html中看到整个页面的副本以及我正在寻找的true值。但如果我尝试将其设置为json,则没有任何反应。我在ColdFusion 9,jQuery 1.6.2。

I'm new to jQuery and am trying to create a login page that will do an Ajax call to a CFC that simply returns true or false on whether login was successfull. My call is making it to the CFC with my arguments correctly, but what's getting returned is the problem. If I set my datatype in jQuery to be "html", I see what looks like a copy of my entire page in html along with the "true" value I'm looking for. But if I try setting it to "json" nothing happens. I'm on ColdFusion 9, jQuery 1.6.2.

我的jQuery函数:

My jQuery function:

$(function() {  
    $('.error').hide();  
    $(".button").click(function() {  

        // validate form here  
        $('.error').hide();  
        var name = $("input#username").val();  
        var pass = $("input#password").val();  
        if (name == "") {  
            $("label#username_error").show();  
            $("input#username").focus();  
            return false;  
        }  
        else if (pass == "") {  
            $("label#password_error").show();  
            $("input#password").focus();  
            return false;  
        }  
        else {
            var bodyContent = $.ajax({
                  url: "cfc/Login.cfc?method=tryLogin&returnFormat=JSON",
                  global: false,
                  type: "POST",
                  data: {username:name,password:pass},
                  dataType: "json",
                  async:false,
                  success: function(msg){
                     alert(msg);
                  }
               }
            ).responseText;

        }
    }); 
});

我的CFC代码,非常简单,我只是想让它现在正确返回:

My CFC code, VERY simple, I'm just trying to get this to return correctly for now:

<cfcomponent name="Login" output="false">
    <cffunction name="tryLogin" access="remote" output="false">
        <cfargument name="username" type="string" required="true"/>
        <cfargument name="password" type="string" required="true"/>
        <cfset var result = true />

        <cfreturn result />
    </cffunction>
</cfcomponent>


推荐答案

当你说:


如果我将jQuery中的数据类型设置为html,我会在html中看到整个页面的副本以及真实值I'我正在寻找。

If I set my datatype in jQuery to be "html", I see what looks like a copy of my entire page in html along with the "true" value I'm looking for.

你的意思是你看到你的真实值后跟ColdFusion调试信息吗?如果是这样,您在网站中使用Application.cfm或Application.cfc吗?如果是Application.cfc,这是AJAX功能的常见问题。您需要在onRequestStart函数中捕获AJAX CFC请求,删除此请求的OnRequest函数,并禁用调试。尝试将此添加到onRequestStart函数:

Do you mean that you see your "true" value followed by ColdFusion debugging info? If so, are you using Application.cfm or Application.cfc in your site? If Application.cfc, this is a common problem with AJAX functionality. You'll need to trap for an AJAX CFC request in your onRequestStart function, remove the OnRequest function for this request, and disable debugging. Try adding this to your onRequestStart function:

<cfif LCase(Right(ARGUMENTS.TargetPage,3)) EQ "cfc">
<cfset StructDelete(THIS,"OnRequest")>
<cfsetting showdebugoutput="no">
</cfif>

这篇关于Ajax jQuery调用ColdFusion组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆