class
SAPO.Communication.Ajax
Version:
Defined in
Communication/Ajax/2.1/lib.js
2.1
Constructor Summary
Function Summary
finish()
Last step after XHR is complete. Call onComplete and
cleanup object.
Loads content from a given url through a XMLHttpRequest. Shortcut function for simple AJAX use cases.
Object
makeError()
Returns new exception object
that can be thrown
Loads content from a given url through a XMLHttpRequest. Shortcut function for simple AJAX use cases.
safeCall()
Safely calls a callback function. Verifies that
the callback is well defined and traps errors
Sets new request header for the subsequent http request
Constructor Details
constructor Ajax
Ajax(String
url, [Object
options])
Creates a new cross browser XMLHttpRequest object
Parameters:
url
- request url
[options]
- request options -
Boolean asynchronous
- if the request should be asynchronous. true by default. -
String method
- HTTP request method. POST by default. -
Object|String parameters
- Request parameters which should be sent with the request -
Number timeout
- Request timeout -
Number delay
- Artificial delay. If request is completed in time lower than this, then wait a bit before calling the callbacks -
String postBody
- POST request body. If not specified, it's filled with the contents from parameters -
String contentType
- Content-type header to be sent. Defaults to 'application/x-www-form-urlencoded' -
Object requestHeaders
- key-value pairs for additional request headers -
Function onComplete
- Callback executed after the request is completed, no matter what happens during the request. -
Function onSuccess
- Callback executed if the request is successful (requests with 2xx status codes) -
Function onFailure
- Callback executed if the request fails (requests with status codes different from 2xx) -
Function onException
- Callback executed if an exception occurs. Receives the exception as a parameter. -
Function onCreate
- Callback executed after object initialization but before the request is made -
Function onInit
- Callback executed before any initialization -
Function onTimeout
- Callback executed if the request times out -
Boolean|String evalJS
- If the request Content-type header is application/json, evaluates the response and populates responseJSON. Use 'force' if you want to force the response evaluation, no matter what Content-type it's using. Defaults to true. -
Boolean sanitizeJSON
- Sanitize the content of responseText before evaluation -
String xhrProxy
- URI for proxy service hosted on the same server as the web app, that can fetch documents from other domains. The service must pipe all input and output untouched (some input sanitization is allowed, like clearing cookies). e.g., requesting http://example.org/doc can become /proxy/http%3A%2F%2Fexample.org%2Fdoc The proxy service will be used for cross-domain requests, if set, else a network error is returned as exception.
Function Details
function evalJSON
Object
evalJSON(String
str, Boolean
sanitize)
Evaluates a given string as json
Parameters:
str
- String to be evaluated
sanitize
- whether to sanitize the content or not
Returns:
Json content as an object
function finish
finish()
Last step after XHR is complete. Call onComplete and
cleanup object.
function isJSON
Boolean
isJSON(String
str)
Checks if a given string is valid json
Parameters:
str
- String to be evaluated
Returns:
True if the string is valid json
function load
Object
load(String
url, Function
callback)
Loads content from a given url through a XMLHttpRequest. Shortcut function for simple AJAX use cases.
Parameters:
url
- request url
callback
- callback to be executed if the request is successful
Returns:
XMLHttpRequest object
function makeError
Object
makeError()
Returns new exception object
that can be thrown
function ping
Object
ping(String
url, Function
callback)
Loads content from a given url through a XMLHttpRequest. Shortcut function for simple AJAX use cases.
Parameters:
url
- request url
callback
- callback to be executed if the request is successful
Returns:
XMLHttpRequest object
function safeCall
safeCall()
Safely calls a callback function. Verifies that
the callback is well defined and traps errors
function setRequestHeader
setRequestHeader()
Sets new request header for the subsequent http request