Detailed description of the Cross-origin problem solution in the vue-cli development environment, vue-cli

Source: Internet
Author: User

Detailed description of the Cross-origin problem solution in the vue-cli development environment, vue-cli

Problems that must be encountered during frontend and backend separation development-Cross-origin. When using vue for development, we started to solve cross-domain problems. CORS (Cross-origin resource sharing) is used ). Add Access-Control-Allow-Origin in the Response Header in the background. In this way, you can call the background interface across domains.

A few days ago, I accidentally saw a proxyTable attribute in the index. js file of config. The configuration can solve the cross-origin Problem of the development environment.

API proxy during development

When you integrate this template with an existing backend, you usually need to access the backend API when using the dev server. To achieve this, we can run the dev server and the API backend in parallel (or remotely), and let the dev server proxy all API requests to the actual backend.

To configure proxy rules, edit dev. proxyTable option config/index. js. The dev server is using http Proxy Middleware for proxy, so you should refer to its documentation for detailed usage. However, this is a simple example:

// Config/index. jsmodule. exports = {//... dev: {proxyTable: {// proxy all requests starting with/api to jsonplaceholder '/api': {target: 'http: // ', changeOrigin: true, pathRewrite: {// The rewrite is required. If processing is performed on the server side, do not use this section '^/api ':''}}}}}

In the preceding example, the proxy request/api/posts/1 is sent to


pathRewrite: { ‘^/api': ‘api' }, 

Send the proxy request/api/posts/1 to

URL matching

In addition to static URLs, you can also use the glob mode to match URLs, such as/api /**. For more information, see context matching. In addition, you can provide an option that filters can be user-defined functions to determine whether a request should be proxied:

proxyTable: { '*': { target: '', filter: function (pathname, req) {  return pathname.match('^/api') && req.method === 'GET' } }}

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: 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.