Vue-based ajax public method (detailed description) and vueajax

Source: Internet
Author: User

Vue-based ajax public method (detailed description) and vueajax

In order to reduce code redundancy, we decided to extract the public method for requesting ajax for colleagues' use.

I used ES6 syntax and wrote this method.

/*** @ Param type: Request type, divided into POST/GET * @ param url request url * @ param contentType * @ param headers * @ param data * @ returns {Promise <any >}*/ajaxData: function (type, url, contentType, headers, data) {return new Promise (function (resolve) {$. ajax ({type: type, url: url, data: data, timeout: 30000, // timeout: 10 seconds headers: headers, success: function (data) {resolve (data) ;}, error: function (XMLHttpRequest, textStatus, errorThrown) {resolve (XMLHttpRequest );}});});}

Return the request result using the callback function.

The test code is as follows:

getAjaxDataMethod: function () {    const url = "";    const type = "POST";    const contentType = "application/json";    const headers = {};    const data = {};    Api.ajaxData(type, url, contentType, headers, data).then(function (res) {     console.log(res);    }).catch(function (err) {     console.log(err);    })   }

Test passed!

The above Vue-based ajax public method (detailed description) is all the content that I have shared with you. I hope to give you a reference and support for the customer's house.

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.