JQuery and Rails 3 ajax: failure callback

I am using Rails 3 and JQuery 1.4.2 and trying to bind to ajax: failure callback for this remote form submission. The callback works fine, however the xhr variable, which went backward, seems to lose the responseText attribute.

This is what my code looks like:

_form.html.haml

= form_for(object, :remote => true) do |f|
  = form fields and such...

Javascript somewhere ...

 $('form').livequery('ajax:loading', function() {
    // what to do on ajax loading
}).livequery('ajax:success', function(data, status, xhr) {

}).livequery('ajax:failure', function(xhr, status, error) {
    alert(xhr.responseText);
});

I basically send object error messages from the controller so that I can display error notifications in this callback. It is strange that I go to rails.js, lines 49-51

error: function (xhr, status, error) {
  el.trigger('ajax:failure', [xhr, status, error]);
}

and manually write responseText to the console, it works as I expected.

Am I doing something wrong? How would an xhr object change from rails.js to my binding?

+3
1

, , . rails.js ajax: failure,

$.ajax({
    url: url,
    data: data,
    dataType: dataType,
    type: method.toUpperCase(),
    ...
    error: function (xhr, status, error) {
        el.trigger('ajax:failure', [xhr, status, error]);
    }
});

,

$('form').livequery('ajax:failure', function(xhr, status, error) {

var responseText, xhr, .

,

console.log(status.responseText)

.

:

('form').livequery('ajax:failure', function(event, xhr, status, error) {

- , .

+6

Source: https://habr.com/ru/post/1762700/


All Articles