I have a post request like
$.post("test", {
ajax: "true",
action: ""
}).done(function(data){
if (data == "ok"){
//xxx
} else if (data == "err"){
//yyy
}
}).fail(function(){
//yyy
});
How to avoid code duplication in the post request if code in .done() method (comment ‘yyy’) the same in fail method (comment ‘yyy’) ??
You can use always callback method, and the request will always go in that block. As you know when data contains error and not, this method will work for server-side errors. And you can catch client-side errors by defining the final else block.