好的,正在努力获得放大解码器。奇怪的问题。如果我有一个附加到我的请求的beforeSend,解码器不会触发。删除beforeSend并解码解码器。
以下是两个例子。
http://jsfiddle.net/sujesharukil/Td2P4/12/
http://jsfiddle.net/sujesharukil/Td2P4/14/
有人能告诉我发生了什么事吗?如果我有一个beforeSend,为什么解码器不会工作?我假设解码器应该在收到请求后触发,所以beforeSend不会对它产生任何影响!
注意:stackoverflow要我在这里发布代码,而不仅仅是fiddles
//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);
}
});
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);
}
});
帮助?
-Suj
答案 0 :(得分: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 : 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,则会发布var ret
在文件中向下,放大侦听此消息
true
true
"request.before.ajax"
所以,如果你有一个beforeSend,如果它没有返回true,那么该消息永远不会被发布,并且解码器永远不会被击中!
解决方案?
从beforeSend函数返回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 );
};
就像一个魅力!希望这有助于其他人试图解决这个问题!
答案 1 :(得分: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." );
}
});