【发布时间】:2019-06-21 12:23:57
【问题描述】:
如何将失败方法调用为 ajax 回调成功方法?例如,当我从服务器获取一些日期时,我不得不说这些数据不正确。为此,我使用以下代码:
Loading.show();
$.post(_api_server+"sign/in.php",opt,function(gets){
try{
if(gets.success){
Toast.show("success!!!","succ");
}else{
// duplicate codes
Loading.hide();
Toast.show("not Ok!!!");
}
}catch(e){
// duplicate codes
Loading.hide();
Toast.show("not Ok!!!");
}
}).fail(function(){
// duplicate codes
Loading.hide();
Toast.show("not Ok!!!");
});
如您所见,我使用重复的代码来做一件事。我想使用这样的东西:
Loading.show();
$.post(_api_server+"sign/in.php",opt,function(gets){
try{
if(gets.success){
Toast.show("success!!!","succ");
}else{
this.fail();
}
}catch(e){
this.fail();
}
}).fail(function(){
Loading.hide();
Toast.show("not Ok!!!");
});
我该怎么做?有什么办法吗?
--更新
我有很多带有不同失败方法的 ajax,我不能使用其他/外部函数。
【问题讨论】: