VUE-CLI Create a project cross-domain request

Source: Internet
Author: User

Problem Description:

Project created with VUE-CLI, development address is localhost:8023, need to access interface on localhost:9000

Analysis Reason:

Access between different domain names requires cross-domain to be requested correctly. There are many ways to cross-domain, often requiring background configuration

However, projects created by VUE-CLI can be directly leveraged by the node. JS Proxy server for cross-domain requests

Solution:

The interface address is originally/form/save, but in order to match the proxy address, add a/api in front

If you use Axios, you can configure a BaseURL globally so that you don't have to asked modify the URL.

Axios.defaults.baseURL = '/api '

Add the configuration item proxytable in Config>index.js Dev:

    Proxytable: {
      '/api ': {
        target: ' http://127.0.0.1:9000/',
        changeorigin:true,
        pathrewrite: {
          ' ^ /api ': '/'}}
    ,

Where '/api ' is the match, target is the requested address

Because the URL of the AJAX prefix '/api ', and the original interface is not the prefix

So we need to rewrite the address via pathrewrite, and convert the prefix '/api ' to '/'

If the interface address itself has '/api ' This generic prefix, you can delete the Pathrewrite

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.