我如何处理Ajax延迟中的错误

时间:2017-11-03 17:48:47

标签: jquery ajax

我如何处理Ajax延迟错误

 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);
    }

我在这里调用Ajax PromiseApi工作正常但是如果有任何错误,我怎么能警告有错误

    var PromiseApi = CallingApi();
    var ErrorPromise = Errorfunction();
    PromiseApi.done(function (data) {
        $.each(data, function (i, value) {
            console.log(value.EmpName);
        })
    })

1 个答案:

答案 0 :(得分:1)

尝试添加此

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);
    });
};