关于jquery:我该如何处理Ajax延迟中的错误

How can i handle Error in Ajax deferred

如何处理Ajax延迟的错误

1
2
3
4
5
6
7
8
9
10
 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,但工作正常,但是如果出现任何错误,我该如何警告出现错误

1
2
3
4
5
6
7
    var PromiseApi = CallingApi();
    var ErrorPromise = Errorfunction();
    PromiseApi.done(function (data) {
        $.each(data, function (i, value) {
            console.log(value.EmpName);
        })
    })


尝试添加此

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
    });
};