Ajax jQuery call to ColdFusion component

倾然丶 夕夏残阳落幕 提交于 2019-12-11 04:05:47

问题


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.

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;

        }
    }); 
});

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>

回答1:


When you say:

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.

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>



回答2:


I thought CF9 changed that behaviour and that you no longer had to use the onRequestStart "hack" for Ajax-called CFCs.

I could of course be wrong - but I thought that was the case...



来源:https://stackoverflow.com/questions/7323069/ajax-jquery-call-to-coldfusion-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!