Use an object to encapsulate the method of repeated ajax calls, and use an object to encapsulate ajax calls

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.