0

How can i handle Ajax deferred error

 function CallingApi() {
      return  $.ajax({
            url: 'http://localhost:10948/Api/Home/GetEmployee123',
            contentType: 'application/x-www-form-urlencoded',
            type:'GET',
        })
    }
    function Errorfunction(xhr) {
        //alert(xhr.status);
    }

Here I'm calling Ajax PromiseApi is working fine but if there is any error, how can I alert there is an Error

    var PromiseApi = CallingApi();
    var ErrorPromise = Errorfunction();
    PromiseApi.done(function (data) {
        $.each(data, function (i, value) {
            console.log(value.EmpName);
        })
    })
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Did you ask Google? You just chain the method `.fail()`.... as in `PromiseApi.fail(function(xhr) { console.log('error', xhr); });` – random_user_name Nov 03 '17 at 17:51
  • Errorfunction does not return a function so you it should not have () – mplungjan Nov 03 '17 at 17:51
  • Duplicate to [https://stackoverflow.com/questions/6792878/jquery-ajax-error-function](https://stackoverflow.com/questions/6792878/jquery-ajax-error-function) – darkknight Nov 03 '17 at 17:53

1 Answers1

1

Try adding this

function CallingApi() {
      var promise = $.ajax({
            url: 'http://localhost:10948/Api/Home/GetEmployee123',
            contentType: 'application/x-www-form-urlencoded',
            type:'GET',
        });
    promise.done(function(data){
        $.each(data, function (i, value) {
            console.log(value.EmpName);
        });
   });
    promise.fail(function(xhr, status, error){
            console.log(xhr.responseText);
    });
};
acaputo
  • 190
  • 12