在发送之前放大解码器和ajax

Suj*_*kil 4 jquery amplifyjs

好的,正在努力获得放大解码器.奇怪的问题.如果我有一个附加到我的请求的beforeSend,解码器不会触发.删除beforeSend并解码器触发.

以下是两个例子.

  1. 没有beforeSend.

http://jsfiddle.net/sujesharukil/Td2P4/12/

  1. 随着发送

http://jsfiddle.net/sujesharukil/Td2P4/14/

有人能告诉我发生了什么吗?如果我有一个beforeSend,为什么解码器不会工作?我假设解码器应该在收到请求后触发,所以beforeSend不应该对它产生任何影响!

注意:stackoverflow希望我在这里发布代码,而不仅仅是小提琴

//please check the fiddles

amplify.request({
    resourceId: "testRequest",
    data: {
        json: JSON.stringify({
            text: 'hello world'
        })
    },
    success: function(data, status) {
        console.log(data, status);
        $('.messages').append('<div> text retrieved: ' + data.text + '</div>');
    },
    error: function(status, xhr) {
        console.log(xhr);
    }
});?
Run Code Online (Sandbox Code Playgroud)

救命?

-Suj

Suj*_*kil 9

好吧,想通了.

在放大这个部分引起了我的注意

beforeSend : function( _xhr, _ajaxSettings ) {

                xhr = _xhr;
                ajaxSettings = _ajaxSettings;
                var ret = defnSettings.beforeSend ?
                    defnSettings.beforeSend.call( this, ampXHR, ajaxSettings ) : true;
                return ret && amplify.publish( "request.before.ajax",
                    defnSettings, settings, ajaxSettings, ampXHR );
            }
        });
Run Code Online (Sandbox Code Playgroud)

var retset设置为true

如果设置为true,则会发布 "request.before.ajax"

在文件中向下,放大侦听此消息

amplify.subscribe( "request.before.ajax", function( resource, settings, ajaxSettings, ampXHR ) {
var _success = ampXHR.success,
    _error = ampXHR.error,
    decoder = $.isFunction( resource.decoder )
        ? resource.decoder
        : resource.decoder in amplify.request.decoders
            ? amplify.request.decoders[ resource.decoder ]
            : amplify.request.decoders._default;

if ( !decoder ) {
    return;
}

function success( data, status ) {
    _success( data, status );
}
function error( data, status ) {
    _error( data, status );
}
ampXHR.success = function( data, status ) {
    decoder( data, status, ampXHR, success, error );
};
ampXHR.error = function( data, status ) {
    decoder( data, status, ampXHR, success, error );
};
Run Code Online (Sandbox Code Playgroud)

});

amplify.request.define("testRequest", "ajax", {

url: "/echo/json/",
dataType: 'json',
type: 'POST',
decoder: function(data, status, xhr, success, error) {
    console.log('decoder fired');
    $('.messages').append('<div>decoder fired </div>');
    success(data);
},
beforeSend: function(xhr){
//not doing anything here, just logging;
    console.log('before send fired');
    $('.messages').append('<div>before send fired </div>');
    return true; //this is the key
}
Run Code Online (Sandbox Code Playgroud)

});

beforeSend : function( _xhr, _ajaxSettings ) {

                xhr = _xhr;
                ajaxSettings = _ajaxSettings;
                var ret = defnSettings.beforeSend ?
                    defnSettings.beforeSend.call( this, ampXHR, ajaxSettings ) : true;
                return ret && amplify.publish( "request.before.ajax",
                    defnSettings, settings, ajaxSettings, ampXHR );
            }
        });
Run Code Online (Sandbox Code Playgroud)

奇迹般有效!希望这有助于其他人试图解决这个问题!