How to access data across domains using Axios in Vue

Source: Internet
Author: User

Recently in the project need to use Axios, so it is bad to fill in the Axios what the hell. The more it is Vue-resource alternative, the official website also said, after all with Axios,

Vue-resource is not being maintained. So how to use this Axios, Internet search a lot of, basically is the same, I do not know whether the authors have been tested in the local. At least I do not follow the online approach, always can not succeed. After a few days of struggle, I finally made it clear, in order not to let other small friends detours, I put in the actual operation of the examples I share to everyone, hope that useful to everyone.

First, install the Axios

The usual, to use Axios, we have to install it, installation method:npm Install Axios

second, the client uses the way

First look at the answer on the Internet, I have already identified on the map, this practice is wrong.


The right approach is Remove Post,:



third, server-side settings

Although the client is set up across domains, but you still cannot access the interface data, you must set the Header property on the server side:


Iv. Axios Method Encapsulation

In general, we will use the method is: Get,post,put,patch, encapsulation method is as follows:



Five, the use of the method after encapsulation

1, in the Main.js file to refer to the previously written files, my name is Http.js


2. Call between places where it is needed:


Description

The Get call method is as follows, where the URL is the interface address

The. $get (URL). Then (res) {

Code

});

The post invocation method is as follows, where the URL is the interface address, and data is the request.

this. $post (Url,data) and then ({

Code

});

The patch invocation method is as follows, where the URL is the interface address and data is the requested

this. $patch (Url,data) and then ({

Code

});

The put call method is as follows, where the URL is the interface address and data is the requested

this. $put (Url,data) and then ({

Code

});

Read the above content, is not very simple, in fact, there is nothing, but this is the problem card for a long time, look at the online answer, really is vulnerable. Problem solved, heart really excited ah O (∩_∩) o ha!

How to access data across domains using Axios in Vue

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.