Last active
December 27, 2015 02:39
-
-
Save fiftyandfifty/7254172 to your computer and use it in GitHub Desktop.
Cross Browser Ajax (success_function needs tweaking)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!(function ($){ | |
"use strict"; | |
window.FF_AJAX = {}; | |
var FF = window.FF_AJAX; | |
/* ================================================================ */ | |
/* */ | |
/* CROSS BROWSER AJAX */ | |
/* */ | |
/* ================================================================ */ | |
FF.isIE8orIE9 = function() { | |
return !!( ( (/msie 8./i).test(navigator.appVersion) || (/msie 9./i).test(navigator.appVersion) ) && !(/opera/i).test(navigator.userAgent) && window.ActiveXObject && XDomainRequest && !window.msPerformance ); | |
}; | |
FF.make_ajax_request = function(url, data, success_function){ | |
if( data === undefined ){ return false;} | |
var result; | |
if ( FF.isIE8orIE9() ) { | |
var xdr = new XDomainRequest(); | |
xdr.open("post", url); | |
xdr.onerror = function() { | |
FF.display_errors(response.error); | |
} | |
xdr.ontimeout = function() { /* only needed for IE9 support */ } | |
xdr.onprogress = function() { /* only needed for IE9 support */ } | |
xdr.onload = function() { | |
var dom = new ActiveXObject("Microsoft.XMLDOM"); | |
dom.async = false; | |
dom.loadXML(xdr.responseText); | |
var response = JSON.parse(dom.parseError.srcText); | |
if(response.success){ FF.handle_response(xdr.responseText, success_function); } | |
else{ FF.handle_response(xdr.responseText, success_function, true); } | |
}; | |
xdr.send(jQuery.param(data)); | |
} | |
else { | |
result = jQuery.ajax({ | |
'type' : 'post', | |
'url' : url, | |
'data' : data, | |
'async' : false, | |
'error' : function(response) { FF.handle_response(response, success_function, true); }, | |
'success' : function(response) { FF.handle_response(response, success_function); } | |
}) | |
} | |
}; | |
FF.display_errors = function(message){ | |
if( typeof(message) != 'string' ){ | |
alert("Error\n\nRemote Connection Error"); | |
} | |
else { | |
alert( "Error\n\n" + message ); | |
} | |
}; | |
FF.handle_response = function(response, success_function, error){ | |
if(success_function === undefined){success_function = null;} | |
if(error === undefined){error = false;} | |
try{ | |
var r = JSON.parse(response); | |
} | |
catch(e){ | |
var r = response; | |
} | |
if(error){ | |
console.log('success', response); | |
if( typeof(r.error) == 'undefined' ){console.log(response);r = {};r.error = {};r.error.message = 'unknown error';} | |
else if( r.error.message === undefined ){console.log(r);r.error.message = r.error;} | |
//FF.display_errors(r.error.message); | |
} | |
else{ | |
if( success_function ){ eval(success_function); } | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment