Skip to main content
Known Participant
June 14, 2009
Answered

Jquery remote with Coldfusion

  • June 14, 2009
  • 1 reply
  • 1629 views

Hi,

I am working with Jquery validation, the plugin is great however I would like to get a remote call to coldfusion to work.

http://docs.jquery.com/Plugins/Validation/Methods/remote#options

Does anyone have an example of this working, I would prefer a call to a CFC method but a cfm would do, I am having problems pasing in the value  and returning it as true/false

Here is the php example

<?php
$request = trim(strtolower($_REQUEST['value']));
$emails = array('glen@marketo.com', 'george@bush.gov', 'me@god.com', 'aboutface@cooper.com', 'steam@valve.com', 'bill@gates.com');
$valid = 'true';
foreach($emails as $email) {
    if( strtolower($email) == $request )
        $valid = 'false';
}
echo $valid;
?>

Thanks

    This topic has been closed for replies.
    Correct answer craigkaminsky

    I'd make my jQuery call look something along the lines of (to keep it simple, I'm just editing the jQuery example code from the URL in the OP):

    $("#myform").validate({
      rules: {
        email: {
          required: true,
          email: true,
          remote: {
            url: "http://mydomain.com/my/path/to/cfcs/mycfc.cfc?method=checkemail&returnformat=json",
            type: "post",
            data: {
              email: '#the_cf_variable_to_check#'
              }
            }
          }
        }
      }
    });

    For my CFC, I'd set up something along he lines of:

    <cfcomponent>

         <cffunction name="checkemail" output="true" returntype="void">

              <cfargument name="email" required="true" type="string" />

              <!--- set the default value to false --->

              <cfset var emails = StructNew() />

              <cfset emails.found = false />

              <!--- code to check email in system --->

              <cfquery name="findemail" datasource="#dsn#">

                   select email from table where email = <cfqueryparam type="cf_sql_varchar" value="#arguments.email#" />

              </cfquery>

              <cfif findemail.RecordCount is 1>

                   <cfset emails.found = true />

              </cfif>

              <cfoutput>#SerializeJSON(emails.found)#</cfoutput>

         </cffunction>

    </cfcomponent>

    From the docs, it states the plugin is expecting JSON in return. To ensure you get JSON, set the returnformat key/value to json in the calling code (jQuery block, above), ensure that your CFC method does output content (output=true) and use the CF function SerializeJSON to convert your structure to JSON when outputting it from your method.

    NOTE: SerializeJSON will convert keys to upper case, so emails.found, when converted to JSON will be emails.FOUND. Initially, I wrote the example CFC to just output the value true or false but changed it to JSON after rereading the plugin docs.

    Hope that helps!

    Craig

    1 reply

    craigkaminskyCorrect answer
    Inspiring
    June 15, 2009

    I'd make my jQuery call look something along the lines of (to keep it simple, I'm just editing the jQuery example code from the URL in the OP):

    $("#myform").validate({
      rules: {
        email: {
          required: true,
          email: true,
          remote: {
            url: "http://mydomain.com/my/path/to/cfcs/mycfc.cfc?method=checkemail&returnformat=json",
            type: "post",
            data: {
              email: '#the_cf_variable_to_check#'
              }
            }
          }
        }
      }
    });

    For my CFC, I'd set up something along he lines of:

    <cfcomponent>

         <cffunction name="checkemail" output="true" returntype="void">

              <cfargument name="email" required="true" type="string" />

              <!--- set the default value to false --->

              <cfset var emails = StructNew() />

              <cfset emails.found = false />

              <!--- code to check email in system --->

              <cfquery name="findemail" datasource="#dsn#">

                   select email from table where email = <cfqueryparam type="cf_sql_varchar" value="#arguments.email#" />

              </cfquery>

              <cfif findemail.RecordCount is 1>

                   <cfset emails.found = true />

              </cfif>

              <cfoutput>#SerializeJSON(emails.found)#</cfoutput>

         </cffunction>

    </cfcomponent>

    From the docs, it states the plugin is expecting JSON in return. To ensure you get JSON, set the returnformat key/value to json in the calling code (jQuery block, above), ensure that your CFC method does output content (output=true) and use the CF function SerializeJSON to convert your structure to JSON when outputting it from your method.

    NOTE: SerializeJSON will convert keys to upper case, so emails.found, when converted to JSON will be emails.FOUND. Initially, I wrote the example CFC to just output the value true or false but changed it to JSON after rereading the plugin docs.

    Hope that helps!

    Craig