AmplifyJS解码器和Ajax beforeSend

4

好的,正在努力获取放大解码器。奇怪的问题。如果我在请求中附加了beforeSend,解码器就不会触发。删除beforeSend,解码器就会触发。

以下是两个示例。

  1. 没有beforeSend。

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

  1. 有beforeSend。

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

有人能告诉我发生了什么事吗?为什么如果有beforeSend解码器就不起作用?我假设解码器应该在接收到请求后触发,因此beforeSend不应该对其产生任何影响!

注意:stackoverflow要求我在此处发布代码,而不仅仅是fiddles。

//请检查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);
    }
});​

Help?

-Suj

2个回答

9

好的,我知道了。

在amplifyjs中,这个部分引起了我的注意。

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 );
            }
        });

请注意,如果指定了beforeSend,则会调用它,否则设置var rettrue
如果将其设置为true,则会发布"request.before.ajax"
在文件的最下方,amplify侦听此消息。
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 );
};

所以,如果你有一个beforeSend函数并且它没有返回true,那么消息就永远不会被发布,解码器也不会被触发!

解决方案?

从你的beforeSend函数中返回true。

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
}

});

这个非常好用!希望这能帮助其他尝试解决此问题的人!


这确实帮了我,谢谢。在我的情况下,我试图定义一个自定义缓存,遇到了同样的问题。找不到任何涵盖此内容的文档。感谢您发布您的研究! - Soulriser

0

我刚从示例页面复制了这个。希望能有所帮助。

amplify.request.decoders.appEnvelope =
    function ( data, status, xhr, success, error ) {
        if ( data.status === "success" ) {
            success( data.data );
        } else if ( data.status === "fail" || data.status === "error" ) {
            error( data.message, data.status );
        } else {
            error( data.message , "fatal" );
        }
    };

amplify.request.define( "decoderExample", "ajax", {
    url: "/myAjaxUrl",
    type: "POST",
    decoder: "appEnvelope" // <--- a function name(string) and not a function.
});

amplify.request({
    resourceId: "decoderExample",
    success: function( data ) {
        data.foo; // bar
    },
    error: function( message, level ) {
        alert( "always handle errors with alerts." );
    }
});

谢谢Stefan的回复。我的东西也是来自他们的文档,就像我说的一样,除了在请求中有beforeSend时不会触发解码器,其他都很好用。所以,不,你的回复没有帮助到我 :( - Sujesh Arukil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接