jquery - Browser returning HTTP 200 with an empty responseText -
i use following code retry operations return http 502, 503 or 504:
/** * @function retrydelayfunction * returns amount of time wait after failure. * * @param {number} retries number of times operation has been retried * @return {number} number of milliseconds wait before retrying operation */ /** * @typedef {object} retryajaxoptions * * @property {number} retries number of times retry failed operation * @param {retrydelayfunction} delayfunction maps failure count delay in milliseconds * @param {array} errorcodes http response codes should trigger retry */ /** * retries http requests using exponential back-off in case of http 502, 503, 504. based on * https://github.com/execjosh/jquery-ajax-retry , http://javadoc.google-http-java-client.googlecode.com/hg/1.15.0-rc/com/google/api/client/util/exponentialbackoff.html * * $.ajax() settings object must contain {@code retry} key enable functionality. * object of type {@link retryajaxoptions} , may used override default behavior. */ function installajaxretries() { "use strict"; /** * nothing. */ var noopfunction = function() { }; var delayinitialintervalms = 250; var delayintervalmultiplier = 1.5; var delayrandomizationfactor = 0.5; /** * @function retrydelayfunction */ var defaultdelayfunction = function(retries) { var retryinterval = delayinitialintervalms * math.pow(delayintervalmultiplier, retries); var delta = retryinterval * delayrandomizationfactor; var min = retryinterval - delta; var max = retryinterval + delta; return (math.random() * (max - min + 1)) + min; }; var min_retries = 1; var default_retries = 3; var default_error_codes = [502, 503, 504]; var default_options = { retries: default_retries, delayfunction: defaultdelayfunction, errorcodes: default_error_codes }; var originalajaxfunction = $.ajax; var ajaxwithretry = function(settings) { settings = $.extend(true, {}, $.ajaxsettings, settings); if (!settings.retry) return originalajaxfunction(settings); var retries = 0; var options = $.extend(true, {}, $.ajaxretrysettings, settings.retry); var originalerrorfunction = settings.error || noopfunction; var originalcompletefunction = settings.complete || noopfunction; // clamp options options.retries = math.max(min_retries, options.retries); options.delayfunction = options.delayfunction || defaultdelayfunction; // override error function settings.error = function(xhr, textstatus, errorthrown) { if ($.inarray(xhr.status, options.errorcodes) < 0 || retries >= options.retries) { // give , call original error() function originalerrorfunction.call(this, xhr, textstatus, errorthrown); return; } // complete() handler retry operation }; // override complete function settings.complete = function(xhr, textstatus) { if ($.inarray(xhr.status, options.errorcodes) < 0 || retries >= options.retries) { // give , call original complete() function originalcompletefunction.call(this, xhr, textstatus); return; } var delayms = options.delayfunction(retries); ++retries; window.settimeout(function() { originalajaxfunction(settings); }, delayms); }; originalajaxfunction(settings); return settings.xhr; }; var ajaxretrysetup = function(options) { default_options = $.extend(true, default_options, options); $.ajaxretrysettings = default_options; return default_options; }; $.ajaxretrysettings = default_options; $.ajaxretrysetup = ajaxretrysetup; $.ajax = ajaxwithretry; } installajaxretries();
ever since started using code, ajax calls began returning http 200 empty responsetext. what's weird first request failing (no retrying taking place) , commenting out code overrides settings.complete
fixes problem. using chrome 29.0.1547.57 m.
why overriding settings.complete
cause problem?
update: control server, know fact never returns http 200 empty response.
update2: can no longer reproduce problem , don't recall did fix it. if can't reproduce in next couple of months plan on closing it.
may trying cross domain server.set response header allow-access-origin
Comments
Post a Comment