【问题标题】:amplifyjs decoders and ajax beforeSendamplifyjs 解码器和 ajax beforeSend
【发布时间】:2012-11-09 17:58:29
【问题描述】:

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

这是两个例子。

  1. 没有 beforeSend。

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

  1. 使用 beforeSend

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

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

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

//请检查小提琴

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

帮助?

-苏杰

【问题讨论】:

    标签: jquery amplifyjs


    【解决方案1】:

    好的,想通了。

    在 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 ret设置为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 );
    };
    

    });

    所以,如果你有一个 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
    }
    

    });

    像魅力一样工作!希望这可以帮助其他人试图解决这个问题!

    【讨论】:

    • 这确实帮助了我,谢谢。就我而言,我试图定义一个自定义缓存,遇到了同样的问题。找不到任何涉及此内容的文档。感谢您发布您的研究!
    【解决方案2】:

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

    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。那是它不会触发解码器的地方。所以不,你的回答没有帮助。 :(
    猜你喜欢
    • 1970-01-01
    • 2013-04-09
    • 2014-08-01
    • 2013-12-25
    • 1970-01-01
    • 2014-03-06
    • 2017-10-21
    • 2012-05-17
    • 1970-01-01
    相关资源
    最近更新 更多