I'm seeing some strange behaviour with my Rails3 app. Note that I'm using the jQuery version of rails.js
From rails.js
starting at line :49
error: function (xhr, status, error) {
el.trigger('ajax:failure', [xhr, status, error]);
alert('error in: ' + xhr.responseText );
}
From my jQuery code to execute after the form.
.bind('ajax:failure', function(xhr, status, error) {
alert('error in: ' + xhr.responseText );
})
In the alert in rails.js shows the expected return text, but the alert my callback shows:
error in: undefined
So it doesn't appear that xhr is being sent to the callback correctly. Am I doing somethin开发者_Python百科g wrong, or is this an issue with rails.js
?
Further testing shows that this applies for both the ajax:failure
and ajax:success
callbacks. With the ajax:success
call back the server response is in status
, so I didn't notice the behavior.
From the jQuery docs on .trigger()
:
"The event object is always passed as the first parameter to an event handler, but if additional parameters are specified during a .trigger() call as they are here, these parameters will be passed along to the handler as well."
The example they give is:
$('#foo').bind('custom', function(event, param1, param2) {
alert(param1 + "\n" + param2);
});
$('#foo').trigger('custom', ['Custom', 'Event']);
So I updated:
.bind('ajax:failure', function(xhr, status, error)
to:
.bind('ajax:failure', function(event, xhr, status, error)
And it's working fine now. Clearly my mistake
精彩评论