我如何处理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);
})
})
答案 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);
});
};