|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
Object | +--AjaxRequest
Defined in ajax.js
| Constructor Summary | |
AjaxRequest
()
Cross-browser implementation to obtain and process a XMLHttpRequest instance. |
|
| Method Summary | |
void
|
doJSONRequest(<String> url, <String> method, <String> parameters, <Function> callback, <Boolean> asynchronous)
Called by the client javascript code to initiate a request that is going to return a JSON text string that is to be converted into a JSON object. |
void
|
doTextRequest(<String> url, <String> method, <String> parameters, <Function> callback, <Boolean> asynchronous)
Called by the client javascript code to initiate a request that is going to return a text string OR a request that returns an XML document that is desired as a string, rather than a DOM object. |
void
|
doXMLRequest(<String> url, <String> method, <String> parameters, <Function> callback, <Boolean> asynchronous)
Called by the client javascript code to initiate a request that is going to return an XML document. |
DOMNode
|
getNode(<String> nodeName, <int> index)
Convenience function that attempts to retrieve an XML DOMNode from the value variable if:
|
DOMNode
|
getNodeByParent(<DomNode> parentNode, <String> nodeName, <int> index)
Convenience function that attempts to retrieve an XML value from the value variable if:
|
int
|
getNodeCount(<String> nodeName)
Convenience function that attempts to retrieve the number of XML elements with the given name that exist in the current value object. |
int
|
getNodeCountByParent(parentNode, <String> nodeName)
Convenience function that attempts to retrieve the number of XML elements with the given name that exist under the parent node. |
int
|
getResponseCode()
Returns the HTTP response code of the last XMHttpRequest made, or null if none have been made. |
Variant
|
getValue()
Convenience function that returns the value returned by the request. |
String
|
getValueForNode(<String> nodeName, <int> index)
Convenience function that attempts to retrieve an XML value from the value variable if:
|
String
|
getValueForNodeByParent(<DomNode> parentNode, <String> nodeName, <int> index)
Convenience function that attempts to retrieve an XML value from the value variable if:
|
Boolean
|
isCompleted()
Convenience method that returns the completed status of the request. |
boolean
|
isIE()
Returns if the last detected browser was IE or not. |
void
|
log(message, severity)
Creates a floating window (if necessary) and adds a message to the log. |
void
|
setDebugging(show)
Turns the debugging window on or off. |
void
|
setDisplayMessage(text)
Sets the display message that is presented while an AjaxRequest is processing. |
XML DOM Object
|
transformValueInto(<String> url, element)
Convenience function that transforms the current XML DOM value based on the XSL file specified in the url, and places it's contents in the suggested DOM element. |
| Constructor Detail |
AjaxRequest()
Cross-browser implementation to obtain and process a XMLHttpRequest instance. It removes the need to worry about cross-browser implementation issues and provides a simple, consistent interface to XMLHttpRequests.
Here's a simple example:
There is a form with two fields, item_no and qty. It also has a span with id price and two buttons, one for checking the price and one to add to the order:
<form action="http://www.mysite.com/add_item.php" method="POST">
<p>Item no.: <input type="text" name="item_no" size="30"/></p>
<p>Qty.: <input type="text" name="qty" size="5"/></p>
<p>Price: <span id="price"></span></p>
<p><input type="button" value="Check Price" onclick="javascript:checkPrice();"/>
<input type="submit" value="Add Item"/></p>
</form>
When the Check Price button is clicked it calls the checkPrice() method. Here's where AJAX magic happens. The checkPrice function looks like this:
var ajax = new AjaxRequest();
function checkPrice() {
String item = document.getElementById("item_no").value;
String qty = document.getElementById("qty").value;
ajax.doXMLRequest("GET", "http://www.mysite.com/check_price?item_no=" + item_no + "&qty=" + qty, updatePrice);
}
The doXMLRequest line will submit the request to the check_price.php script, using the GET method. It will also pass control to your updatePrice() method once completed.
When the request has completed, the XML document has been stored and your updatePrice method is called. The updatePrice method can obtain reference to the XML DOM object by simply calling getValue(). (If we had used getTextRequest instead of getXMLRequest, we would get a String variable by calling getValue()).
For this example we'll assume an XML document something like this:
<data>
<item_no>123456</item_no>
<qty>1000</qty>
<price>10.50</price>
</data>
We then simply parse the XML and update the price:
function updatePrice() {
var xml = ajax.getValue();
var root = xml.documentElement;
var item_no = root.getElementsByTagName('item_no')[0].firstChild.data;
var qty = root.getElementsByTagName('qty')[0].firstChild.data;
var price = root.getElementsByTagName('price')[0].firstChild.data;
document.getElementById("item_no").value = item_no;
document.getElementById("qty").value = qty;
document.getElementById("price").innerHTML = "$" + price;
}
That's about it. All we have to do is:
| Method Detail |
void doJSONRequest(<String> url, <String> method, <String> parameters, <Function> callback, <Boolean> asynchronous)
Called by the client javascript code to initiate a request that is going to return a JSON text string that is to be converted into a JSON object. The resulting object will be stored in the value variable and can be retrieved using getValue();
url - The url to the server resource that will handle the request. (REQUIRED)
method - The HTTP method to send the request. Must be "GET" or "POST". Default is "GET" (OPTIONAL)
parameters - A url formatted list of parameter names and values. Parameters are specified as name=value&name=value.... Default is null. (OPTIONAL)
callback - The end-user defined method that will be called upon successful completion of the request. Default is null. (OPTIONAL)
asynchronous - . Set to true if the request is to be asynchronous, false if not. Default is true. (OPTIONAL)
void doTextRequest(<String> url, <String> method, <String> parameters, <Function> callback, <Boolean> asynchronous)
Called by the client javascript code to initiate a request that is going to return a text string OR a request that returns an XML document that is desired as a string, rather than a DOM object. The resulting string will be stored in the value variable and can be retrieved using getValue();
url - The url to the server resource that will handle the request. (REQUIRED)
method - The HTTP method to send the request. Must be "GET" or "POST". Default is "GET" (OPTIONAL)
parameters - A url formatted list of parameter names and values. Parameters are specified as name=value&name=value.... Default is null. (OPTIONAL)
callback - The end-user defined method that will be called upon successful completion of the request. Default is null. (OPTIONAL)
asynchronous - . Set to true if the request is to be asynchronous, false if not. Default is true. (OPTIONAL)
void doXMLRequest(<String> url, <String> method, <String> parameters, <Function> callback, <Boolean> asynchronous)
Called by the client javascript code to initiate a request that is going to return an XML document. The resulting DOM object will be stored in the value variable and can be retrieved using getValue();
url - The url to the server resource that will handle the request. (REQUIRED)
method - The HTTP method to send the request. Must be "GET" or "POST". Default is "GET" (OPTIONAL)
parameters - A url formatted list of parameter names and values. Parameters are specified as name=value&name=value.... Default is null. (OPTIONAL)
callback - The end-user defined method that will be called upon successful completion of the request. Default is null. (OPTIONAL)
asynchronous - . Set to true if the request is to be asynchronous, false if not. Default is true. (OPTIONAL)
DOMNode getNode(<String> nodeName, <int> index)
Convenience function that attempts to retrieve an XML DOMNode from the value variable if:
Returns - The DOMNode of the 'index' instance of 'nodeName' or null if it doesn't exist.
nodeName - The name of the node.
index - The index. i.e. If there are expected to be 3 nodes with the name 'item', then you would enter 0 for the first instance, 1 for the second instance and 2 for the third instance. If no index is provided then 0 is assumed.
DOMNode getNodeByParent(<DomNode> parentNode, <String> nodeName, <int> index)
Convenience function that attempts to retrieve an XML value from the value variable if:
Returns - DOMNode of the 'index' instance of 'nodeName' with 'parentNode' as it's parent, or null if it doesn't exist.
parentNode - The parent DOMNode under which the child node should be found. See #getNode
nodeName - The name of the node
index - The index. i.e. If there are expected to be 3 nodes with the name 'item', then you would enter 0 for the first instance, 1 for the second instance and 2 for the third instance. If no index is provided then 0 is assumed.
int getNodeCount(<String> nodeName)
Convenience function that attempts to retrieve the number of XML elements with the given name that exist in the current value object. This assumes that
nodeName - The node name to search for.
int getNodeCountByParent(parentNode, <String> nodeName)
Convenience function that attempts to retrieve the number of XML elements with the given name that exist under the parent node. This assumes that
nodeName - The node name to search for.
int getResponseCode()
Returns the HTTP response code of the last XMHttpRequest made, or null if none have been made.
Returns - The response code of the last XMLHttpRequest made, or null.
Variant getValue()
Convenience function that returns the value returned by the request. Returns - a String or XML DOM Object based on the type of request made.
String getValueForNode(<String> nodeName, <int> index)
Convenience function that attempts to retrieve an XML value from the value variable if:
Returns - The value of the 'index' instance of 'nodeName' or null if it doesn't exist.
nodeName - The name of the node
index - The index. i.e. If there are expected to be 3 nodes with the name 'item', then you would enter 0 for the first instance, 1 for the second instance and 2 for the third instance. If no index is provided then 0 is assumed.
String getValueForNodeByParent(<DomNode> parentNode, <String> nodeName, <int> index)
Convenience function that attempts to retrieve an XML value from the value variable if:
Returns - The value of the 'index' instance of 'nodeName' or null if it doesn't exist.
parentNode - The parent DOMNode under which the child node should be found. See #getNode
nodeName - The name of the node
index - The index. i.e. If there are expected to be 3 nodes with the name 'item', then you would enter 0 for the first instance, 1 for the second instance and 2 for the third instance. If no index is provided then 0 is assumed.
Boolean isCompleted()
Convenience method that returns the completed status of the request. Returns - true if readyState = 4, otherwise false.
boolean isIE()
Returns if the last detected browser was IE or not.
Returns - True if IE was last detected, false otherwise.
void log(message, severity)
Creates a floating window (if necessary) and adds a message to the log.
message - A message to add to the log window.
void setDebugging(show)
Turns the debugging window on or off.
show - True or false.
void setDisplayMessage(text)
Sets the display message that is presented while an AjaxRequest is processing.
text - The message to display.
XML DOM Object transformValueInto(<String> url, element)
Convenience function that transforms the current XML DOM value based on the XSL file specified in the url, and places it's contents in the suggested DOM element.
Returns - a transformed XML string or null if the XSL could not be found.
url - The url of the XSL document to use for transformation.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||