Created
September 26, 2012 22:14
-
-
Save edwardbeckett/3790964 to your computer and use it in GitHub Desktop.
Nested Ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(function () { | |
$.ajax({ | |
type:'GET', | |
data:{ | |
method:"getAllStates" | |
}, | |
url:'./com/remote.cfc', | |
dataType:'json', | |
success:function (data) { | |
/*Lets do that again ...*/ | |
$.ajax({ | |
type:'GET', | |
data:{ | |
method:"getCities", | |
state:"FL" | |
}, | |
url:'./com/remote.cfc', | |
dataType:'json', | |
success:function (data) { | |
$.each(data, function (k, v) { | |
console.log(this); | |
}) | |
} | |
}) | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfset application.cw.dsn = 'national'> | |
<cffunction name="getAllStates" access="remote" returnType="string" returnformat="JSON"> | |
<!--- Define variables ---> | |
<cfset var qStates=""> | |
<!---<cfset var result=ArrayNew(2)>---> | |
<cfset var result=arrayNew(2) /> | |
<cfset var i=0> | |
<!--- Get data ---> | |
<cfquery name="qStates" datasource="#application.cw.dsn#"> | |
SELECT State, StateFullName | |
FROM states | |
GROUP BY State | |
ORDER BY StateFullName ASC | |
</cfquery> | |
<!--- Convert results to array ---> | |
<cfloop index="i" from="1" to="#qStates.RecordCount#"> | |
<cfset result[i][1]=qStates.State[i]> | |
<cfset result[i][2]=qStates.StateFullName[i]> | |
</cfloop> | |
<cfset result = serializeJSON(result) /> | |
<!--- And return it ---> | |
<cfreturn result> | |
</cffunction> | |
<cffunction name="getCities" access="remote" returnType="string" returnformat="JSON"> | |
<cfargument name="state" required="true" /> | |
<!--- Define variables ---> | |
<cfset variables.state = arguments.state /> | |
<cfset var qCounties = ""> | |
<cfset var result = arrayNew(2)/> | |
<cfset var i = 0> | |
<!--- Get data ---> | |
<cfquery name="qCities" datasource="#application.cw.dsn#"> | |
SELECT DISTINCT CountyName, CityAliasName | |
FROM states | |
WHERE state = '#variables.state#' | |
ORDER BY CountyName, CityAliasName ASC | |
</cfquery> | |
<!--- Convert results to array ---> | |
<cfloop index="i" from="1" to="#qCities.RecordCount#"> | |
<cfset result[i][1]=qCities.CountyName[i]> | |
<cfset result[i][2]=qCities.CityAliasName[i]> | |
</cfloop> | |
<cfset result = serializeJSON(result) /> | |
<!--- And return it ---> | |
<cfreturn result> | |
</cffunction> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment