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