среда, марта 21, 2012

jQuery 1.7.1 & Streaming

Убил времени, пока не нашел

http://stackoverflow.com/questions/6035987/can-i-use-jquery-prefilters-to-detect-onreadystatechange-events-where-readystate

Рабочий код

$.ajaxPrefilter(function( options, _, jqXHR ) {
    if ( options.onreadystatechange ) {
        var xhrFactory = options.xhr;
        options.xhr = function() {
            var xhr = xhrFactory.apply( this, arguments );
            function handler() {
                options.onreadystatechange( xhr, jqXHR );
            }
            if ( xhr.addEventListener ) {
                xhr.addEventListener( "readystatechange", handler, false );
            } else {
                setTimeout( function() {
                    var internal = xhr.onreadystatechange;
                    if ( internal ) {
                        xhr.onreadystatechange = function() {
                            handler();
                            internal.apply( this, arguments ); 
                        };
                    }
                }, 0 );
            }
            return xhr;
        };
    }
});
    
function log(s) {
    $( "body" ).append( s + "<br>" );
}

function xhrStreaming() {

    $.ajax({
        type: "GET",
        url: '/echo/json',
        cache: false,
        onreadystatechange: function( xhr ) {
            log( "(" + xhr.readyState + ") '" + xhr.responseText + "'" );
        },
        success: function( data ) {
            log( "ajax success" );
        },
        error: function( _, _, e ) {
            log( "ajax error" );
        }
    });
}


xhrStreaming();

Комментариев нет:

Мой список блогов