Simon
Simon

Reputation: 629

Ajax-submitted form, no callback triggered

I have a form in a bootstrap modal, that I submit with Ajax. Up to this part, everything's good, the form is submitted and the datas are saved in the database. But, after this, no callback functions are triggered, neither complete, success nor error. I really struggle to see where this comes from.

Controller:

def create
@param = EventParam.new(event_param_params)

respond_to do |format|
  if @param.save
    format.json { render json: @param }
  else
    format.html { render :new }
    format.json { render json: @mail_template.errors, status: :unprocessable_entity }
  end
end

end

JS:

$(".event-param-form").submit(function(e){
          e.preventDefault();
          var valuesToSubmit = $(this).serialize();
          $.ajax({
                  type: "POST",           
                  url: $(this).attr('action'),
                  data: valuesToSubmit,
                  dataType: "JSON",
          });
    }); 

    $('.event-param-form').bind('ajax:complete', function() {
           console.log("test");
           $("#myModal").modal('hide');
    });

Note that I also tried to use the callback success/complete from $.ajax() with no results.

Upvotes: 0

Views: 94

Answers (1)

Gowtham Selvaraj
Gowtham Selvaraj

Reputation: 478

You can use success method in ajax itself

 $(".event-param-form").submit(function(e){
          e.preventDefault();
          var valuesToSubmit = $(this).serialize();
          $.ajax({
                  type: "POST",           
                  url: $(this).attr('action'),
                  data: valuesToSubmit,
                  dataType: "JSON",
                  success : function(data){
                     //Success Call back
                    console.log(data);
                    alert('success');
                    $("#myModal").modal('hide');
                  },
                  error  :function(jqXHR, textStatus, errorThrown) {
                    // Error Callback
                  }
          });
    }); 

Upvotes: 1

Related Questions