4

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

这是两个例子。

  1. 没有 beforeSend。

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

  1. 使用 beforeSend

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

帮助?

-苏杰

4

2 回答 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.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
}

});

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

于 2012-11-09T18:27:06.953 回答
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." );
    }
});
于 2012-11-09T18:04:55.180 回答