AJAX is often used to call remote data in projects. Every time it is called, an ajax method must be written. This results in too much repeated code and insufficient readability. Therefore, I usually encapsulate it. Call when needed
AJAX is often used to call remote data in projects. Every time it is called, an ajax method must be written, which results in too much repeated code and insufficient readability. Therefore, I usually encapsulate it and call it when needed.
var imgUpload = { //ajax请求数据 method:function(murl,mdata,method,success){ $.ajax({ type: method, url: murl, dataType : "jsonp", data: mdata, timeout: 20000, error: function (data) { console.log(data); alert("请求失败"); }, success: function (data) { //console.log(data); success?success(data):function(){}; } }); } } //调用 imgUpload.method("url","","get",function (data) { if(data.code == 0){ alert(data); }else{ alert("请求失败"); } });
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Ajax submission form page refreshes quickly solution
Two solutions for Ajax opening a new window and being intercepted by the browser
The above is the detailed content of Use objects to encapsulate ajax repeatedly called methods. For more information, please follow other related articles on the PHP Chinese website!