Open
Description
I think that the httpRequest implementation is very standar today, so $.ajax is a very big implementation with a lot of features.
Thinking about use iris in mobile context, it could be better to make an standard implementation only for JSON, such as we are currently using in some projects:
function _instanceHttpReq(){
var httpRequest;
if ( window.XMLHttpRequest ) {
httpRequest = new XMLHttpRequest();
}
else if ( window.ActiveXObject ) {
try {
httpRequest = new ActiveXObject('MSXML2.XMLHTTP');
}
catch (err1) {
try {
httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (err2) {
if ( window.console && window.console.error ) {
console.error('Fatal error', err2);
}
}
}
}
if ( !httpRequest ) {
if ( window.console && window.console.error ) {
console.error('Fatal error, object httpRequest is not available');
}
}
httpRequest.overrideMimeType('application/json');
httpRequest.withCredentials = true;
return httpRequest;
}
function _asyncJson(p_method, p_url, f_success, p_data, p_cache) {
var req = _instanceHttpReq();
var cache = p_cache || true;
var url = p_url;
if ( cache ) {
url += '?_='+(new Date().getTime());
}
req.open(p_method, url, true);
req.onreadystatechange = function() {
if ( req.readyState === 0 || req.readyState === 4 ){
if (req.status === 200) {
if ( req.responseText !== null ) {
f_success( null, JSON.parse(req.responseText) );
}
else {
f_success(null, null);
}
}
else {
f_success({ "status" : req.status, req : req },null);
}
}
};
if(p_data !== null) {
if ( typeof p_data === "string" ) {
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
}
else {
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
p_data = JSON.stringify(p_data);
}
}
req.send(p_data);
}