vue used front-end proxy solve cross-domain

How to solve cross-domain with front-end proxy

We know that same-origin policy only exists in the browser, it does not exist in the server. Therefore, we need cross-domain requests will be forwarded to the address of our own servers and server commission to request information. (Common with nginx forwards, node agent.)

Vue commonly used in the project is proxyTable, this is easy to use.

Open the config below index.js, find proxyTable, add the following code:

'/api': { //替换代理地址名称
      target: 'http://api.douban.com/', //代理地址
     changeOrigin: true, //可否跨域
     pathRewrite: {
     '^/api': '' //重写接口,去掉/api
    }
}

(I.e. a / api replace http://api.douban.com/)
proxyTable position as follows:

Once you've configured under the need to restart the project npm run dev

After the restart, you can call, the cross-domain

Specific use:

Before the interface need to call plus "/ api" to

Guess you like

Origin www.cnblogs.com/HelloJC/p/11277623.html