JSONRequest.js

JSONRequest implementation

This object is based on JSONRequest “official” draft and allows developers to perform get or post Ajax requestes in a simple way.

With 3 public methods it’s possible to manage more than a single request, using a queue to order requestes and to preserve server interactions.

To know more about JSONRequest, please visit this page: http://www.json.org/JSONRequest.html

Version

0.9probably requires more debug

Compatibility

FireFoxVersion 1, 1.5, 2 and 3 (FireFox uses secure code evaluation)
Internet ExplorerVersion 5, 5.5, 6 and 7
Opera8 and 9 (probably 7 too)
SafariVersion 2 (probably 1 too)
KonquerorVersion 3 or greater

Dependencies

JSONRequestError.js

Credits

Author

Andrea Giammarchi, http://www.3site.eu

License

Copyright (C) 2007 Andrea Giammarchi - www.3site.eu

Permission is hereby granted, free of charge,
to any person obtaining a copy of this software and associated
documentation files (the "Software"),
to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Summary
This object is based on JSONRequest “official” draft and allows developers to perform get or post Ajax requestes in a simple way.
Personal JSONRequest implementation with queue and multiple exception filters.
blocks a request and call user callback if request has been successful blocked adding 10 milliseconds as delay time for next interaction.
prepares a request and push them inside queue.
prepares a request and push them inside queue.
removes timeout and block XHR interaction.
adds arbitrary delay time to internal variable.
checks every user get or post method arguments.
interval callback, performs one request using queue and next available index.
creates a new XMLHttpRequest or ActiveX object.

JSONRequest

JSONRequest

Personal JSONRequest implementation with queue and multiple exception filters.

Example

try {
JSONRequest.get("mypage.php", function(sn, response, exception){
alert(exception || response);
});
}
catch(e) {
alert(e);
}

Methods - Public

Summary
blocks a request and call user callback if request has been successful blocked adding 10 milliseconds as delay time for next interaction.
prepares a request and push them inside queue.
prepares a request and push them inside queue.

cancel

this.cancel = function(i)

blocks a request and call user callback if request has been successful blocked adding 10 milliseconds as delay time for next interaction.

Arguments

NumberA valid JSONRequest serial number.

Example

mybtn.serialNumber = JSONRequest.get("page?something", function(sn, result, error){alert(result)});
mybtn.onclick = function(){
JSONRequest.cancel(this.serialNumber);
};

get

this.get = function(url,
done)

prepares a request and push them inside queue.  Throws a JSONRequest Exception if some parameter is wrong.

Arguments

StringA valid url to call using Ajax respecting Same Origin Policy.
FunctionA callback with 3 arguments: serialNumber, responceObject, exceptionObject

Returns

NumberA new valid serial number.

Example

JSONRequest.get("page.psp?var=value", function(sn, result, error){alert([sn, result, error])});

Note

If callback has not 3 arguments this method will throw an exception.  If request has not problems callback is called using only 2 arguments, serialNumber and responseObject.  This method uses default JSONRequest timeout, 10 seconds.

post

this.post = function(url,
send,
done,
timeout)

prepares a request and push them inside queue.  Throws a JSONRequest Error if some parameter is wrong.

Arguments

StringA valid url to call using Ajax respecting Same Origin Policy.
Array / ObjectData to send
FunctionA callback with 3 arguments: serialNumber, responceObject, exceptionObject
[Number]optional milliseconds timeout.  Default: 10000

Returns

NumberA new valid serial number.

Example

JSONRequest.post("page.psp?var=value", {name:"Andrea"}, function(sn, result, error){alert([sn, result, error])});

Note

Server side will recieve a JSONRequest key with escaped JSON data (using encodeURIComponent).  If data is not an Array, an Object or a valid JSON compatible variable, this method throws a JSONRequest Exception.

Methods - Private

Summary
removes timeout and block XHR interaction.
adds arbitrary delay time to internal variable.
checks every user get or post method arguments.
interval callback, performs one request using queue and next available index.
creates a new XMLHttpRequest or ActiveX object.

cancel

function cancel(l,
JSONRequestError)

removes timeout and block XHR interaction.  Changes queue index properties to save memory.

Arguments

NumberA valid JSONRequest serial number.
[JSONRequestError]optional dedicated error

changeDelay

function changeDelay(a,
b)

adds arbitrary delay time to internal variable.

Arguments

Numbermilliseconds to add
[Number]optional random milliseconds to add

prepare

function prepare(url,
send,
done,
timeout)

checks every user get or post method arguments.  Throws different JSONRequest Exceptions if these are not valid.

Arguments

StringA valid url to call using Ajax respecting Same Origin Policy.
Array / ObjectData to send
FunctionA callback with 3 arguments: serialNumber, responceObject, exceptionObject
[Number]optional milliseconds timeout.  Default: 10000

request

function request()

interval callback, performs one request using queue and next available index.

xhr

function xhr()

creates a new XMLHttpRequest or ActiveX object.

Returns

new XMLHttpRequest or new Microsoft.XMLHTTP ActiveX object

Properties - Private

Private

List

Number’delay’ - delay time for next interaction.  Each error, except for cancel one, add a delay of 500 + rand(0, 511) milliseconds.
Number’l’ - queue index, used to perform one request each time.
String’method’ - temporary method used with request
Array’queue’ - a list of objects used for each interaction.
RegExp’re’ - basic Regular Expression to verify url
JSONRequest
Personal JSONRequest implementation with queue and multiple exception filters.
this.cancel = function(i)
blocks a request and call user callback if request has been successful blocked adding 10 milliseconds as delay time for next interaction.
this.get = function(url,
done)
prepares a request and push them inside queue.
this.post = function(url,
send,
done,
timeout)
prepares a request and push them inside queue.
function changeDelay(a,
b)
adds arbitrary delay time to internal variable.
function prepare(url,
send,
done,
timeout)
checks every user get or post method arguments.
function request()
interval callback, performs one request using queue and next available index.
function xhr()
creates a new XMLHttpRequest or ActiveX object.