好的,想通了。
在 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
}
});
奇迹般有效!希望这可以帮助其他人试图解决这个问题!