jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 浏览器返回带有空responseText的HTTP 200大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码重试返回http 502,503或504的操作:

/**
 * @function RetryDelayFunction
 * Returns the amount of time to wait after a failure.
 *
 * @param {number} retries the number of times the operation has been retried
 * @return {number} the number of milliseconds to wait before retrying the operation
 */

/**
 * @typedef {Object} RetryAjaxOptions
 *
 * @property {number} retries the number of times to retry a Failed operation
 * @param {RetryDelayFunction} delayFunction maps a failure count to a delay in milliseconds
 * @param {Array} errorCodes the http response codes that should trigger a retry
 */

/**
 * Retries http requests using an exponential BACk-off in case of http 502,503,504. Based on
 * https://github.com/execjosh/jquery-ajax-retry and http://javadoc.google-http-java-client.googlecode.com/hg/1.15.0-rc/com/google/api/client/util/ExponentialBACkOff.html
 *
 * The $.ajax() setTings object must contain a {@code retry} key to enable this functionality.
 * This object is of type {@link RetryAjaxOptions} and may be used to override the default behavior.
 */
function installAjaxRetries()
{
    "use Strict";
    /**
     * Do 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,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 up and call the original error() function
                originalErrorFunction.call(this,xhr,errorThrown);
                return;
            }
            // The complete() handler will retry the operation
        };

        // Override complete function
        setTings.complete = function(xhr,textStatus)
        {
            if ($.inArray(xhr.status,options.errorCodes) < 0 || retries >= options.retries)
            {
                // Give up and call the original complete() function
                originalCompleteFunction.call(this,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();

自从我开始使用这段代码以来,一些AJAX调用开始返回带有空responseText的http 200.奇怪的是,第一个请求失败(实际上没有重试),只是注释掉覆盖设置的代码.完成修复问题.我使用的是Chrome 29.0.1547.57 m.

为什么重写设置会导致此问题?

更新:我控制服务器,所以我知道它永远不会返回带有空响应的http 200.

更新2:我再也无法重现这个问题,我不记得我做了什么修复它.如果我在接下来的几个月里无法重现它,我打算关闭它.

解决方法

可能您正在尝试跨域server.set响应标头到allow-access-origin

大佬总结

以上是大佬教程为你收集整理的jquery – 浏览器返回带有空responseText的HTTP 200全部内容,希望文章能够帮你解决jquery – 浏览器返回带有空responseText的HTTP 200所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。