The ability to create server-side ActionScript provides a familiar way for Flash developers to access ColdFusion query and HTTP features without learning CFML. You can place ActionScript files (.asr) that you want to call from the Flash application on the server, anywhere under the web server's root directory. To specify subdirectories of the webroot or a virtual directory, use package dot notation. For example, in the following assignment code, the stockquotes.asr file lives in the mydir\stock\ directory:
stockService = gatewayConnnection.getService("mydir.stock.stockquotes", this);
You can also point to virtual mappings, such as cfsuite.asr.stock.stockquotes, where cfsuite is a virtual mapping and asr.stock is a subdirectory of that mapping. The CF.query and CF.http functions give you a well-defined interface for building the SQL queries and HTTP operations of ColdFusion.
For example, the following server-side ActionScript function definition returns a RecordSet object:
function basicQuery()
{
mydata = CF.query({datasource:"customers",
sql:"SELECT * FROM myTable"});
return mydata;
}
The CF.http ActionScript function lets you retrieve information from a remote HTTP server. HTTP Get and Post methods are supported. Using the Get method, you send information to the remote server directly in the URL. This method is often used for a one-way transaction in which the CF.http function retrieves an object, such as the contents of a web page. The Post method can pass variables to a form or CGI program, and can also create HTTP cookies.
One of the most basic and useful ways of using the CF.http function is using the Get method argument to retrieve a page from a specified URL. For example, the following server-side code retrieves file content from the specified URL:
function basicGet(url)
{
// Invoke with just the url. This is an http get.
result = CF.http(url);
return result.get("Filecontent");
}
In the client-side ActionScript, you call the service function and display the results in the Flash application, as in the following example:
#include "NetServices.as"
if (inited == null)
{
inited = true;
cfserver = NetServices.createGatewayConnection("http://localhost/
flashservices/gateway");
myHttpService = gatewayConnnection.getService("httpFuncs", this);
}
myHttpService.basicGet("http://www.macromedia.com");
function basicGet_Result(result)
{
myDisplayScreen.text = result;
}
The CF.http function returns an object that contains properties (also known as attributes) that you reference to access the contents of the file returned, header information, HTTP status codes, and so on. The following table shows the properties available:
The arguments in the following table can be passed only as an array of objects in the params argument of the CF.http function:
| Argument |
Description |
|---|---|
| name |
Variable name for data that is passed |
| type |
Transaction type:
|
| value |
Value of URL, FormField, Cookie, File, or CGI variables that are passed |
You can write the CF.http function using either named arguments or positional arguments. The positional argument style supports a subset of CF.http arguments, although the named argument style is more readable than the positional argument style.
The CF.http function accepts the following arguments using the named argument style:
CF.http
({
method:"get or post",
url:"URL",
username:"username",
password:"password",
resolveurl:"yes or no",
params:arrayvar,
path:"path",
file:"filename"
})
The named argument style uses curly braces to surround the function arguments. The positional argument approach supports a subset of CF.http arguments, but it lets you code in a more succinct and efficient style. The schema for the positional argument style is as follows:
CF.http(url);
CF.http(method, url); CF.http(method, url, username, password); CF.http(method, url, params, username, password);
When using positional arguments, do not use curly braces.
The CF.query function lets you perform queries against any ColdFusion data source. The CF.query function maps closely to the cfquery CFML tag, although it currently supports a subset of the cfquery attributes.
You use the CF.query function to perform the following actions:
You can write the CF.query function using either named arguments or positional arguments. The named argument style is a more readable style than the positional argument style. Although the positional argument style supports a subset of CF.query arguments, it allows a more compact coding style that is appropriate for simple expressions of the CF.query function.
The CF.query function accepts the following arguments using the named argument style:
CF.query
({datasource:"data source name",sql:"SQL stmts",username:"username",password:"password",maxrows:number,timeout:milliseconds})
The named argument style uses curly braces to surround the function arguments. The positional argument approach supports a subset of CF.query arguments, but it lets you code in a more succinct and efficient style. The schema for the positional argument style is as follows:
CF.query(datasource, sql);
CF.query(datasource, sql, maxrows); CF.query(datasource, sql, username, password); CF.query(datasource, sql, username, password, maxrows);
When using positional arguments, do not use curly braces.
The CF.query function returns a RecordSet object to Flash. For more information about working with RecordSet objects, see Chapter 3, "Using Flash Remoting Data in ActionScript".