Using Flash Remoting MX with server-side ActionScript

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;
}

Using CF.http

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:
Property
Description
Text
A Boolean value that indicates whether the specified URL location contains text data.
Charset
The character set used by the document specified in the URL.
HTTP servers normally provide this information, or the charset is specified in the charset parameter of the Content-Type header field of the HTTP protocol. For example, the following HTTP header announces that the character encoding is EUC-JP:
Content-Type: text/html; charset=EUC-JP
Header
Raw response header. For example, macromedia.com returns the following header:
HTTP/1.1 200 OK
Date: Mon, 04 Mar 2002 17:27:44 GMT
Server: Apache/1.3.22 (Unix) mod_perl/1.26
Set-Cookie: MM_cookie=207.22.48.162.4731015262864476;
path=/; expires=Wed, 03-Mar-04 17:27:44 GMT;
domain=.macromedia.com
Connection: close
Content-Type: text/html
Filecontent
File contents, for text and MIME files.
Mimetype
MIME type. Examples of MIME types include text/html, image/png, image/gif, video/mpeg, text/css, and audio/basic.
Responseheader
Response header. If there is one instance of a header key, you can access the value as a simple type. If there is more than one instance, values are put in an array in the responseHeader structure.
Statuscode
HTTP error code and associated error string, which returns the following HTTP status codes:
400: Bad Request
401: Unauthorized
403: Forbidden
404: Not Found
405: Method Not Allowed

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:
  • URL
  • FormField
  • Cookie
  • CGI
  • File
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.

Using CF.query

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".