JQuery Ajax and ColdFusion

I am trying to submit a request to the ColdFusion page. I would like the ColdFusion page to return true or false based on a successful login.

When you click on the login button:

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 username and returns, but I don’t know how to handle what it returns? I am very new to ColdFusion.

 <cffunction "TryLogin" returntype="boolean"> </cffunction> 

.. I am not sure how to return data from a function after its authentication, but to read it alone after its return. Has anyone dealt with this before?

Thanks George

+4
source share
3 answers

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

I usually like to post a more structured answer, so you have the option of returning additional information to the user, such as an error message, but the following simple example is true / false. You can simply give the method a remote access attribute, for example:

 <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 have your CFC, your main script should work 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 are returning a complex data type, such as a structure or an array, you need to specify returnFormat for "json" and change your arg data, for example:

 data: {method: 'tryLogin', returnFormat: 'json', username: userName, password: password} 
+6
source

I don’t know about cold fusion bit, but you should post this data, preferably via SSL, jquery for publication will 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 } }); }; 
0
source

You can use cfreturn from the inside to return the result.

Your snippet will look 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 returned, you may need to specify returnFormat in your ajax post.

0
source

Source: https://habr.com/ru/post/1303187/


All Articles