Reply class

From PhiWiki
Jump to: navigation, search

The reply class contains all data related to a server reply including cookies, content data and content type.

Usage

The reply class is a global object in the server scripting environment.

loadModule('reply');
var r=reply;

Additionally you can use the reply class to set cookies with reply.setCookie().

Properties

  • String charset [read|write]
  • String content [read|write]
  • String contenttype [read|write]
  • String filename [read|write]

String charset

Sets the charset in which the unicode string (from JavaScript) will be encoded. This should reflect also the content type charset part (text/plain; charset=iso-8859-1).

reply.charset='iso-8859-1';

Default is 'utf-8'.

String contenttype

If you want to return user defined data you may set the content type:

reply.contenttype='text/plain; charset=utf-8';

The default is 'text/html; charset=utf-8'.

String content

Sets the content data.

reply.content='my data which should be sent to the browser';

Warning: you can not set an empty content, if you set an empty content the original content (ie. created by the server) will be restored. Use at least a space character.

String filename

You can set a file name. This file will be sent to the browser. The content type will be automatically detected by the appropriate mime type and can not be overwritten, ie. reply.contentType will be ignored.

reply.filename='C:/path_to_file/myfile.txt';

If the file is in the document root of the server (and the server module is enabled), you can use:

reply.filename=server.documentroot+'/myfile.txt';

See also: server.documentroot.

Functions

String percentDecoded()

Returns a percent decoded string.

var dec=reply.percentDecoded( '%3D' );
// dec='=';

String percentEncoded()

Returns a percent encoded string.

var enc=reply.percentEncoded( '=' );
// enc='%3D'

Void setCookie( Date )

Sets a cookie in the client browser with a specific expiry date.

reply.setCookie( name, value, expires, path="/", domain="", secure=false, discard=false );

The attributes name, value and expires are mandatory. The other attributes are optional.

var expiry=new Date();
expiry.setTime( expiry.getTime()+(1000*60*60*24) );
reply.setCookie( 'mycookie', 'myvalue', expiry );

The example above sets a cookie which expires after 24 hours.

Void setCookie( Number )

Sets a cookie in the client browser which expires after maxage seconds.

reply.setCookie( name, value, maxage, path="/", domain="", secure=false, discard=false );

The attributes name, value and maxage are mandatory. The other attributes are optional.

reply.setCookie( 'mycookie', 'myvalue', 60*60 );

The example above sets a cookie which expires after 1 hour.

Additional notes

Currently binary data can only be sent by specifing a file name.

See also: reply.filename.