JQ's ajax encapsulation
function Ajax(opt){ var isdefult=opt.isdefult||"need"; // Determine whether this function is needed to judge, or to judge after the data returned by the page request is returned $.ajax({ url: 'http://haha/'+opt.url, // The path of the data interface dataType:'json' , type: opt.type ||"get", // The default method of the request is get data:opt.param||"", // The default parameter of the request is empty async:opt.async|| true , // Whether it is Asynchronous, the default is asynchronous timeout: 10000 , success: function (res) { if (isdefult=="need"){ // Determine whether this function is needed to judge, or after the data requested by the page comes back if (res.code==0){ // According to Judging by the return of your own project opt.successBack instanceof Function&&opt.successBack(res.data); // successful function, see the value of your own project } else { console.log(res.msg) } } else { opt.successBack instanceof Function&&opt.successBack(res); } }, error: function(xhr, type, errorThrown){ console.log(JSON.stringify(xhr)); console.log(type); console.log(errorThrown); } }); } See what you like. You can choose to extend it to jquery, or it can be a function.
How to use: Simple get request: Ajax({ "url":"get/user","successBack":function(data){
// some processing after success
} }) post request: Ajax({ "url":"get/user","type":"post", "param":{"user":"xg"}, "successBack":function(data){
//成功之后的一些处理
} })