Use an object to encapsulate the method of repeated ajax calls, and use an object to encapsulate ajax calls
Remote Data is often called using AJAX in projects. Each call requires an ajax method, which leads to too much code duplication and poor readability, I usually encapsulate them and call them when needed.
Var imgUpload = {// ajax request data method: function (murl, mdata, method, success) {$. ajax ({type: method, url: murl, dataType: "jsonp", data: mdata, timeout: 20000, error: function (data) {console. log (data); alert ("request failed") ;}, success: function (data) {// console. log (data); success? Success (data): function () {}}}) ;}// call imgUpload. method ("url", "", "get", function (data) {if (data. code = 0) {alert (data) ;}else {alert ("request failed ");}});
The preceding figure shows how to use an object to encapsulate repeated ajax calls. I hope this will be helpful to you.
Articles you may be interested in:
- Javascript encapsulation source code for an AJAX Automatic completion function [Chinese supported]
- An encapsulated Ajax class
- JavaScript encapsulates the data code passed by Ajax
- Self-encapsulated ajax
- How ajax works and how asynchronous requests are encapsulated
- Reencapsulate ajax in Jquery to simplify the operation example
- Share an AJAX function encapsulated by native Javascript
- AJAX encapsulation User Guide