This Question have 1 answers right now.

Calling another function after call.fail()

Tags: jquery ajax
By : SpM
Source: Stackoverflow.com
Question!

I'm new to jQuery and I'm not sure if this is even do-able. I'm using ajax to get the data from a sharepoint list- function LoadNumbers(a,b) with call.done and call.fail functions. Is there a way I can call another function to load data if LoadNumbers() function fails?

function LoadNumbers(a, b) {
  var url = _spPageContextInfo.webAbsoluteUrl
  var call = $.ajax({
    url: url + "/_api/Web/Lists/GetByTitle('xyz')/items?$select=1, 2,3 &$filter=(Num1 eq '" + VNum + "')and(Num2 eq '" + PNum + "')&$orderby=ID asc&$top=5000",
    type: "GET",
    dataType: "json",
    headers: {
      Accept: "application/json;odata=verbose"
    }
  });
  call.done(function(data, textStatus, jqXHR) {
    $('#example').dataTable({
      "bDestroy": true,
      "bProcessing": true,
      "aaData": data.d.results,
      "aoColumns": [{
        "mData": "abc"
      }, {
        "mData": "xyz"
      }, {
        "mData": "pqr"
      }, ],
      dom: 'Bfrtip',
      buttons: [
        'excel', 'print'
      ]
    });
  });
  call.fail(function(jqXHR, textStatus, errorThrown) {
    alert("Error retrieving Tasks: " + jqXHR.responseText);
  });
}

By : SpM


Answers

Just call the other function in the .fail function.

call.fail(function(jqXHR, textStatus, errorThrown) {
    alert("Error retrieving Tasks: " + jqXHR.responseText);
    otherFunction(a, b);
});
By : Barmar


Video about Calling another function after call.fail()