JavaScript - AJAX Functions
The base function that all JpLib ajax functions use is the utility function executeAjax. If you don't want to do things manually, please skip ahead to the JpLib.ajax functions below.
executeAjax
:::javascript
function executeAjax(callback, url, parameters, data, timeout, type, username, password)
Parameters
| Param | Type | Description |
|---|---|---|
| callback | Function | The function that will be called when status changes occur with the HTTPRequest object. |
| url | String | The URL to call. |
| parameters | String | The parameters to post to the URL. |
| data | Object | (Optional) An object that can be tied to the HTTPRequest object for reference in the callback function. |
| timeout | Integer | (Optional) Number of milliseconds to wait before timing out and aborting the request. |
| type | String | (Optional) MIME type override. |
| username | String | (Optional) The username to use for authentication on the server side. If not specified, then the authentication on the server side depends upon the already instantiated sessions. |
| password | String | (Optional) (see notes for the username parameter) |
Returns
The HTTPRequest object that was generated to make the call.
Notes
The callback function is executed each time the status of the HTTPRequest object changes. A simple callback function reference that can be passed to the callback parameter would work as follows:
:::javascript
var execCallback = function() {
// "this" is the HTTPRequest object.
if (this.readyState == 4) {
// A readyState 4 means that the call is done and the response has been completely received.
if (this.status == 200) {
// Status 200 means a normal, successful "OK" response. Do stuff here.
}
else {
// Any other status means something abnormal happened, line an error. Do other stuff here.
}
}
};
JpLib.ajax.htmlCallback
The purpose of this function is to process the response of an AJAX call when the intended successful response is HTML code. When a successful response is received, the response text is set in the innerHTML of the container element identified by the containerid parameter. When an error response is received, an error dialog is displayed with the response text as the message.
:::javascript
function JpLib.ajax.htmlCallback(request, containerid, callback, errorMessage, closeParent)
Parameters
| Param | Type | Description |
|---|---|---|
| request | HTTPRequest | The request object from which to process the response. |
| containerid | String | The ID of a container element (likely a DIV tag) in which innerHTML to place the response text upon a successful response. |
| callback | Function | (Optional) A callback function which, if specified, will be called after a successful response is received and processed. |
| errorMessage | String | (Optional) A message to use in the error dialog, if needed. This overrides the default value of using the response text. |
| closeParent | Element | (Optional) An element whose parent dialog is to be closed after a successful response is received and processed, and after the callback is executed, if specified. |
Returns
No return value.
JpLib.ajax.htmlAction
The purpose of this function is to execute an AJAX request to the URI (action). Internally this function uses the executeAjax utility function to make the call, and the JpLib.ajax.htmlCallback function to process the response.
:::javascript
function JpLib.ajax.htmlAction(containerId, action, params, data, message, callback)
Parameters
| Param | Type | Description |
|---|---|---|
| containerid | String | The ID of a container element (likely a DIV tag) in which innerHTML to place the response text upon a successful response. |
| action | String | The URL to call. |
| params | String | (Optional) The parameters to post when calling the URI. |
| data | Object | (Optional) An object to store on the HTTPRequest object. |
| message | String | (Optional) A message to display in a flash dialog after a successful response is received and processed. |
| callback | Function | (Optional) A callback function which, if specified, will be called after a successful response is received and processed. |
Returns
No return value.
JpLib.ajax.messageCallback
The purpose of this function is to process the response of an AJAX call when the intended successful response is a message for use in a dialog. When a successful response is received, the response text is set displayed in an alert dialog. When an error response is received, an error dialog is displayed with the response text as the message.
:::javascript
function JpLib.ajax.messageCallback(request, callback)
Parameters
| Param | Type | Description |
|---|---|---|
| request | HTTPRequest | The request object from which to process the response. |
| callback | Function | (Optional) A callback function which, if specified, will be called after a successful response is received and processed. |
Returns
No return value.
JpLib.ajax.messageAction
The purpose of this function is to execute an AJAX request to the URI (action). Internally this function uses the executeAjax utility function to make the call, and the JpLib.ajax.messageCallback function to process the response.
:::javascript
function JpLib.ajax.messageAction(action, params, data)
Parameters
| Param | Type | Description |
|---|---|---|
| action | String | The URL to call. |
| params | String | (Optional) The parameters to post when calling the URI. |
| data | Object | (Optional) An object to store on the HTTPRequest object. |
Returns
No return value.