Describes how to request cross-origin requests for projects created by Vue-cli.

Source: Internet
Author: User

Describes how to request cross-origin requests for projects created by Vue-cli.

Problem description:

For a project created using Vue-cli, the development address is localhost: 8023. You need to access the interface on localhost: 9000.

Cause analysis:

Cross-origin requests are required for access between different domain names. There are many cross-origin methods, which usually require background configuration.

However, projects created by Vue-cli can directly use the Node. js proxy server to implement cross-origin requests.

 Solution:

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

If axios is used, you can configure a baseURL globally, so you do not need to modify the url one by one.

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 ': '/'}}},

The '/api' is the matching item, and the target is the requested address.

Because the prefix '/api' is added to the ajax url, and the original interface does not have this prefix

Therefore, we need to rewrite the address through pathRewrite and convert the prefix '/api' '/'

If the interface address has the generic prefix '/api', you can delete pathRewrite.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.