jquery ajax finally code example

Example 1: finally in ajax call

$.ajax({
        type: "GET",
        dataType: dataType,
        contentType: contentType,
        async: TRUE,
        url: $('html form:nth-child(1)').attr('action') + "?" $('html form:nth-child(1)').serialize(),
        success: function(data) {
            console.log("FUNFOU!");
        },
        error: function(data) {
            console.log("NÃO FUNFOU!");
        },
        complete: function(data) {
            console.log("SEMPRE FUNFA!"); 
            //A function to be called when the request finishes 
            // (after success and error callbacks are executed). 
        }
    });

Example 2: jquery ajax on fail

fail: function(xhr, textStatus, errorThrown){
	alert('request failed');
}

Example 3: jquery ajax promise

$.ajax({
    url: "/someurl",
    method: "GET",
    data: { 
      a: "a"
  })
  .done(function(data) {
    console.log('success callback 1', data) 
  })
  .done(function(data) {
    console.log('success callback 2', data) 
  })
  .fail(function(xhr) {
    console.log('error callback 1', xhr);
  })
  .fail(function(xhr) {
    console.log('error callback 2', xhr);
  });