• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

AJAX to CFC: Callback issue...

Explorer ,
Oct 08, 2013 Oct 08, 2013

Copy link to clipboard

Copied

Hello,

Sending an AJAX call with jQuery to a ColdFusion Component.

The data is returned. But the callback-function suggest that the Boolean value True equals False in the .ajax method. And I don't get it.

Help me Obi-Wan.

See video: http://youtu.be/GlNj3WzYjiI

TOPICS
Advanced techniques

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 08, 2013 Oct 08, 2013

Copy link to clipboard

Copied

You might want to actually post some of the code here rather than people having to watch the entire video clip to comment on it.

A few things:

Your CFC can be simplified.  You don't need the YesNoFormat() call in your CFIF tag - CF treats all non-zero numbers as "true" values automatically.  So you could just have

<CFIF Q.RecordCount>

You don't have a returntype attribute on your function, so ColdFusion doesn't know you want to return JSON (which is probably what jQuery is expecting).  You can either add the returntype attribute, or add SerializeJSON() in your <CFRETURN> tags.

You might need to add the "dataType" setting to your $ajax() call too.

$.ajax({

     ...

     ...

     dataType: "json",

     ...

});

HTH,

Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2013 Oct 08, 2013

Copy link to clipboard

Copied

Thank you for looking at my problem.

Adding dataType in the ajax-call prevents the alert() from happening at all.

Previously, I had mucked about with returntype="json" in the CFC and/or serializing the returned data, which in this case is the boolean value true/false, 1/0, yes/no, etc, even changing the compareson and returned value as text-strings - that's pretty desperate. Doesn't matter. I can wear a fake mustach and glasses; still doesn;t matter. The data is returned, but I still get the callback-function in the ajax-request wrong; true or false is always false, or alert() or console.log does not exist at all.

I moved away from defining returntype="json" in CFC after browsing through some samples; think it was Raymond Camden that may had mentioned that it might be wise to let the caller deside what the returnformat should be rather than the CFC, but I could be wrong about that.

The logic leading to the <cfreturn true> or <cfreturn false> does not have any relevance. I wish it did, so it would be worth arguing about, but it isn't.

So, on popular demand, here is the code. Best Regards. ///johan

Member.cfc.............................

...

<cffunction name="emailExists" access="remote" output="no">

  <cfargument name="Email" type="string" required="no" default="xxx@yyy.zzz">

        <cfquery name="Q" datasource="#datasource_r#" username="#username_r#" password="#password_r#">

            SELECT member_id, Email

            FROM tbl_member

            WHERE Email = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Email#">;

        </cfquery>

        <cfif yesNoFormat(Q.RecordCount)>

            <cfreturn true>

  <cfelse>

        <cfreturn false>

        </cfif>

</cffunction>

---------------------------------------

TestPage.cfm

<cfsetting showdebugoutput="no">

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script type="text/javascript">

$(document).ready(function() {

  checkEmailUnique = function checkEmail(theEmail){

  $.ajax({

  type:'get',

  url: 'Member.cfc',

  data: {method: 'emailExists', Email:theEmail},

  success: function(returnData) {

  if (returnData == true) { alert('Email already exists'); } else { alert('Address is unique'); }

  }

        });

  };

});

</script>

<cfform name="myform">

  <cfinput type="text" name="Email" onchange="checkEmailUnique(this.value)">

</cfform>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 08, 2013 Oct 08, 2013

Copy link to clipboard

Copied

LATEST

I can only say that I've passed boolean JSON values back to JavaScript via AJAX without seeing the issue you are seeing.  As to Ray's recommendation about not specifying a returntype, I'd say that is useful advise if you are building an API.  However, if you are doing a tightly coupled CF/JavaScript application, I don't think being explicit in the format expectation is a bad thing.

If you don't want to quibble about the YesNoFormat thing, that's fine.  Just keep in mind that YesNoFormat() is a presentation format function, and is not necessary in the context you are using it (unless you were doing <CFIF YesNoFormat(q.RecordCount) IS "YES">.

At this point, it would be helpful to take a look at the actual response from the AJAX call in the browser's console (Firebug or Chrome Developer tools) to see what the format of the response is.

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation