Home > Web Front-end > JS Tutorial > Use objects to encapsulate ajax repeatedly called methods

Use objects to encapsulate ajax repeatedly called methods

亚连
Release: 2018-05-24 11:22:02
Original
1855 people have browsed it

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("请求失败");
}
});
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax post request jump page

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template