Whatever message this page gives is out now! Go check it out!
<cfajaxproxy
cfc = "CFC name"
jsclassname = "JavaScript proxy class name">
OR
<cfajaxproxy
bind = "bind expression"
onError = "JavaScript function name"
onSuccess = "JavaScript function name"><cfscript>
cfajaxproxy(cfc = "CFC name",
jsclassname = "JavaScript proxy class name")
</cfscript>
OR
<cfscript>
cfajaxproxy
(bind = "bind expression",
onError = "JavaScript function name",
onSuccess = "JavaScript function name")
</cfscript>Attribute | Req/Opt | Default | Description |
bind | A bind or cfc attribute is required | A bind expression that specifies a CFC method, JavaScript function, or URL to call. For detailed information about specifying bind expressions, see Binding data to form fields in the Developing ColdFusion Applications. You cannot use this attribute with the cfc attribute. | |
cfc | A bind or cfc attribute is required | The CFC for which to create a proxy. Specify a dot-delimited path to the CFC. The path can be an absolute filepath, or relative to location of the CFML page. For example, if the myCFC CFC is in the cfcs subdirectory of the ColdFusion page, specify cfcs.myCFC. If the CFC extends another CFC, the extended CFC methods are also available to the JavaScript proxy.On UNIX-based systems, the tag searches first for a file whose name or path corresponds to the specified name or path, but is in all lowercase. If it does not find it, ColdFusion then searches for a filename or path that corresponds to the attribute value exactly, with identical character casing. This attribute cannot be used with the bind attribute. | |
jsclassname | Optional | The value of the cfc attribute | The name to use for the JavaScript proxy class that represents the CFC. This attribute cannot be used with a bind attribute. |
onError | Optional | The name of a JavaScript function to invoke when a bind, specified by the bind attribute fails. The function must take two arguments: an error code and an error message. This attribute cannot be used with a cfc attribute. | |
onSuccess | Optional | The name of a JavaScript function to invoke when a bind, specified by the bind attribute succeeds. The function must take one argument, the bind function return value. If the bind function is a CFC function, the return value is automatically converted to a JavaScript variable before being passed to the onSuccess function. This attribute cannot be used with a cfc attribute. |
The proxy passes a _CF_NODEBUG Boolean argument to called CFC functions. ColdFusion checks this value, and when it is true, does not append to the response any debugging information that it normally would send. This behavior ensures that the JSON responses to AJAX requests do not include any non-JSON (i.e., debug information) text. |
Function | Description |
setAsyncMode() | Sets the call mode to asynchronous. The calling thread (the Java thread of the client system that is processing the page) is not blocked when you make a call to a proxy function, so page processing can continue while waiting for a response from the server. The proxy invokes the function specified by the setCallbackHandler function with the response from the server. If an error occurs, the proxy invokes the error handler specified by the setErrorHandler function. |
setCallbackHandler(function) | Specifies the callback handler for an asynchronous call. The function parameter is the JavaScript function to invoke as an argument. The callback function must take one parameter, the return value from the CFC that the proxy has deserialized from JSON to a JavaScript representation. This method automatically sets the call mode to asynchronous. |
setErrorHandler(function) | Sets the error handler that the proxy invokes if there is an error in an asynchronous call. The function parameter is the JavaScript function to invoke. The error handler function must take two parameters:
|
setForm(ID) | Adds names and values of the fields in the form specified by the ID attribute to the arguments passed by a proxy function that is called immediately after this function. For more information, see Submitting data to a CFC in the Developing ColdFusion Applications. |
setHTTPMethod("method") | Sets the HTTP method to use for the call. The method parameter is a case-insensitive string, and must have one of the following values:
|
setQueryFormat(format) | Specifies the JSON format in which to return ColdFusion query data. The parameter must have one of the following values:
|
setReturnFormat(format) | Specifies the format in which the CFC function returns the result. ColdFusion automatically converts the function return value into the specified format before returning it to the client. The parameter must have one of the following values:
|
setSyncMode() | Sets the call mode to synchronous (the default synchronization mode). The calling thread remains blocked until the call returns. If an error occurs, the proxy throws an exception. In synchronous mode, the methods in the CFC proxy return the CFC method results directly to the caller. |
<!--- The cfajaxproxy tag creates a client-side proxy for the emp CFC.
View the generated page source to see the resulting JavaScript.
The emp CFC is in the components subdirectory of the directory that
contains this page. --->
<cfajaxproxy cfc="components.emp" jsclassname="emp">
<html>
<head>
<script type="text/javascript">
// Function to find the index in an array of the first entry
// with a specific value.
// It is used to get the index of a column in the column list.
Array.prototype.findIdx = function(value){
for (var i=0; i < this.length; i++) {
if (this[i] == value) {
return i;
}
}
}
// Use an asynchronous call to get the employees for the
// drop-down employee list from the ColdFusion server.
var getEmployees = function(){
// create an instance of the proxy.
var e = new emp();
// Setting a callback handler for the proxy automatically makes
// the proxy's calls asynchronous.
e.setCallbackHandler(populateEmployees);
e.setErrorHandler(myErrorHandler);
// The proxy getEmployees function represents the CFC
// getEmployees function.
e.getEmployees();
}
// Callback function to handle the results returned by the
// getEmployees function and populate the drop-down list.
var populateEmployees = function(res)
{
with(document.simpleAJAX){
var option = new Option();
option.text='Select Employee';
option.value='0';
employee.options[0] = option;
for(i=0;i<res.DATA.length;i++){
var option = new Option();
option.text=res.DATA[i][res.COLUMNS.findIdx('FIRSTNAME')]
+ ' ' + res.DATA[i][[res.COLUMNS.findIdx('LASTNAME')]];
option.value=res.DATA[i][res.COLUMNS.findIdx('EMP_ID')];
employee.options[i+1] = option;
}
}
}
// Use an asynchronous call to get the employee details.
// The function is called when the user selects an employee.
var getEmployeeDetails = function(id){
var e = new emp();
e.setCallbackHandler(populateEmployeeDetails);
e.setErrorHandler(myErrorHandler);
// This time, pass the employee name to the getEmployees CFC
// function.
e.getEmployees(id);
}
// Callback function to display the results of the getEmployeeDetails
// function.
var populateEmployeeDetails = function(employee)
{
var eId = employee.DATA[0][0];
var efname = employee.DATA[0][1];
var elname = employee.DATA[0][2];
var eemail = employee.DATA[0][3];
var ephone = employee.DATA[0][4];
var edepartment = employee.DATA[0][5];
with(document.simpleAJAX){
empData.innerHTML =
'<span style="width:100px">Employee Id:</span>'
+ '<font color="green"><span align="left">'
+ eId + '</font></span><br>'
+ '<span style="width:100px">First Name:</span>'
+ '<font color="green"><span align="left">'
+ efname + '</font></span><br>'
+ '<span style="width:100px">Last Name:</span>'
+ '<font color="green"><span align="left">'
+ elname + '</font></span><br>'
+ '<span style="width:100px">Email:</span>'
+ '<font color="green"><span align="left">'
+ eemail + '</span></font><br>'
+ '<span style="width:100px">Phone:</span>'
+ '<font color="green"><span align="left">'
+ ephone + '</font></span><br>'
+ '<span style="width:100px">Department:</span>'
+ '<font color="green"><span align="left">'
+ edepartment + '</font></span>';
}
}
// Error handler for the asynchronous functions.
var myErrorHandler = function(statusCode, statusMsg)
{
alert('Status: ' + statusCode + ', ' + statusMsg);
}
</script>
</head>
<body>
<!--- The form to display the employee drop-down list and
employee data. --->
<form name="simpleAJAX" method="get">
List of Employees:
<select name="employee" onChange="getEmployeeDetails(this.value)">
<script language="javascript">
getEmployees();
</script>
</select>
<br><br>
<span id="empData"></span>
</form>
</body>
</html><cfcomponent>
<cfset this.dsn = "cfdocexamples">
<cffunction name="getEmployees" access="remote" returnFormat="json"
output="false">
<cfargument name="empid" required="no" type="string" default="0">
<cfquery name="qryEmp" datasource="#this.dsn#">
select * from Employees
<cfif empid neq 0>
where Emp_ID = #empid#
</cfif>
</Cfquery>
<cfreturn qryEmp>
</cffunction>
</cfcomponent>