views:

302

answers:

3

I am attempting to submit a querystring to a ColdFusion page. I would like the ColdFusion page to return true or false based on whether the login in successful.

When my login button is clicked:

   function AttemptLogin(userName, password)
   {
       $.ajax({
                url: 'login.cfc&user=' + userName + '&' + 'password=' + password,
                success: function(data) {
                $('.result').val();
                 [Check for true or false here.]
                }
       });
   };

My ColdFusion page authenticates the password and user name, and returns, but I don't know how to process what it's returning? I am very new to ColdFusion.

<cffunction "TryLogin" returntype="boolean">

</cffunction>

..I'm not sure how to return data from the function after it authenticates, yet alone read it once it returns. Anyone dealt with this before?

Thanks, George

A: 

I don't know about the cold fusion bit, but you should post this data, preferably over SSL, the jquery to post would look like this:

function AttemptLogin(userName, password)
{
   $.ajax({
            url: 'login.cfc'
            type: 'POST',
            data: "{'user':'" + userName + "', 'password':'" + password + "'}",
            success: function(data) {
              if(data === "true") //server returns simple "true" or an error message
                alert("Success")
              else
                alert(data); //the error message from a failed login
            }
   });
};
Nick Craver
You shouldn't need to concatenate username/password like that: data: {user: userName, password: password } should work just fine.
Shawn Grigson
@Shawn - Just habit I guess, used to forming valid JSON style everywhere.
Nick Craver
I was wondering if that was the reason. I dunno, whenever I'm writing JavaScript I tend to stick to a standard internal JSON notation. If I was planning to parse that JSON string, then I'd definitely want the keys quoted. In any case, just a minor quibble/preference on my part. It will work just fine as is.
Shawn Grigson
@Shawn - I think since jQuery 1.4 kicked my ass on some non-compliant json coming from a few services I had to make perfect, I've gotten more into this mode as a side-effect...damn you JSON
Nick Craver
JavaScript permits, but does not require, quoted keys. JSON requires quoted keys. JSON is a very carefully designed subset of JavaScript. Not all JavaScript is valid JSON. See http://json.org/ for the spec.
Justice
@Nick - I think I understand the differences now. If you pass the data arg over as data: {username: userName, password: password} you will get two fields/args posted. Your method, I believe, will post a single argument in JSON format which will then need to be parsed. In your case, you'd need valid JSON. I generally do posts without surrounding the data arg in quotes, which submits multiple fields. Personal preference, really. Sorry for being pedantic. :)
Shawn Grigson
@Justice - Yes you're correct...I was saying it's a quote habit I've gotten into because of having to make everything 100% valid JSON since 1.4 came out, not that they're necessarily related.
Nick Craver
A: 

You can use cfreturn from within a cffunction to return a result.

Your snippet becomes something like this:

<cffunction name="TryLogin" returntype="boolean" output="false"> 
  <cfargument name="user" type="string" required="true" />
  <cfargument name="password" type="string" required="true" />

  <cfset var loggedIn = false />

  <!--- check the database, return a record that matches the details, etc --->
  <cfif query.recordCount eq 1>
    <cfset var loggedIn = true />
  </cfif>
  <cfreturn loggedIn />
</cffunction> 

Depending on the type of variable you are returning you may need to specify a returnFormat in your ajax post.

Antony
+4  A: 

Are you submitting a query string, or a form post? Usually a login is a POST, not a GET. But anyway.

I usually like to post a more structured response, so that you have the possibility to return additional information to the user, like an error message, but the simple true/false example follows. You could just give the method a remote access attribute, like so:

<cfcomponent name="Login">
   <cfset variables.dsn = "mydb" />
   <cffunction name="tryLogin" access="remote" output="false" returntype="boolean">
      <cfargument name="username" type="string" required="true"/>
      <cfargument name="password" type="string" required="true"/>

      <cfset var loginQuery = "" />

      <cfquery name="loginQuery" datasource="#variables.dsn#">
         SELECT * 
         FROM users 
         WHERE 
            username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#"/> 
            AND 
            password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#"/>
      </cfquery>

      <cfif loginQuery.recordcount>
         <cfreturn true />
      <cfelse>
         <cfreturn false />
      </cfif>
   </cffunction>
</cfcomponent>

Now that you've got your CFC, your basic script should work just fine, with a few modifications:

function AttemptLogin(userName, password)
   {
       $.ajax({
                url: 'login.cfc',
                data: {method: 'tryLogin', username: userName, password: password},
                success: function(data) {
                   if (data == true) { alert('true!');} else { alert('false!');}
                }
       });
   };

As mentioned in another answer, if you're returning a complex datatype, like a struct or array, you'll need to specify a returnFormat of 'json' and modify your data arg, like so:

data: {method: 'tryLogin', returnFormat: 'json', username: userName, password: password}
Shawn Grigson