Cross-domain request agent and Axios of Vue.js learning

Source: Internet
Author: User

Vue.js Learning cross-domain request agent and Axios: cross-domain Request proxy

1: Open Config/index.js

module.exports{    Dev: {
    }}

Find proxytable{} here, instead:

Proxytable: {      '/api ': {        target: ' http://121.41.130.58:9090 ',//Set the interface domain name and port number you are calling do not forget to add the HTTP        changeorigin: True,        pathrewrite: {          ' ^/api ': '//Here is understood to use '/api ' instead of the address inside the target, and in the later component we drop the interface directly with the API instead of such as I want to call ' http:// 40.00.100.100:3002/user/add ', directly write '/api/user/add ' can        }      }    }

2: Use this in the component that needs to tune the interface:

Axios.post ('/api/yt_api/login/dologin ', postdata). Then (function (response) {Console.log (1) console.log (response);}). catch (function (error) {Console.log (error);})

 Note: The original interface: Http://http://121.41.130.58:9090/yt_api/login/doLogin

Page Call: http://localhost:8081/api/yt_api/login/dologin-- There's a/api/here. Not superfluous, do not delete

Two:Axios Pass

1:vue official recommended component Axios the default is to commit the JSON string, so we're going to use the JSON string directly in the form behind the URL, directly to the JSON object in the line.

Let PostData = {' Companycode ': ' Tur ', ' userName ': ' 123456789123456789 ', ' Password ': ' 123456 '}axios.post ('/api/yt_api/' Login/dologin ', postdata). Then (function (response) {Console.log (1) console.log (response);}). catch (function (error) {Console.log (error);});

Here we will postdata this JSON object into the Post method, in the form of the page:

2: In the form of Key:val

Let PostData = Qs.stringify ({companycode: ' tur ', UserName: ' 123456789123456789 ', Password: ' 123456 '})

We need to operate on this JSON object, here's the QS you need to install first

NPM Install QS

Re-Import

Import qs from ' QS '

The form in the polygon is:

Cross-domain request agent and Axios of Vue.js learning

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.