Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vue] vue-cli怎么解决跨域的问题? #487

Open
haizhilin2013 opened this issue Jun 22, 2019 · 5 comments
Open

[vue] vue-cli怎么解决跨域的问题? #487

haizhilin2013 opened this issue Jun 22, 2019 · 5 comments
Labels
vue vue

Comments

@haizhilin2013
Copy link
Collaborator

[vue] vue-cli怎么解决跨域的问题?

@haizhilin2013 haizhilin2013 added the vue vue label Jun 22, 2019
@xn213
Copy link

xn213 commented Jun 23, 2019

dev > 代理 devServer.proxy
pro > ng( 待 todo )

@boundless-qi
Copy link

axios 设置代理

@LonHon
Copy link

LonHon commented Jul 4, 2019

vue-cli 主要在本地通过本地服务器拦截转发请求的模式解决跨域问题。

步骤:

  1. config中设置proxy,这步决定哪种命名规则(比如'/abc/'开头的请求)的请求将被拦截(个人以为是通过改造XMLHttpRequest对象)到本地跨域服务器
  2. 本地服务器转发请求到目标服务器
  3. 本地服务器设置允许跨域的headers,然后返回结果,从而解决跨域

跨域问题,除了单机修改浏览器配置外,都是服务器端配合进行解决。

@liuxiaoyang1
Copy link

在根目录下新建:vue.config.js注意名不能错误,然后里面配置
module.exports = {
devServer: {
proxy: {
//配置跨域
'/api': {
target: '跨域url',
ws: true,
changOrigin: true
// pathRewrite: {
// '^/api': ''
// }
}
}
}
}

@ajh99990
Copy link

ajh99990 commented Dec 3, 2019

vue-cli无法解决跨域问题。真正解决跨域问的是webpack。只不过vue-cli3.0将webpack的配置继承到了vue.config.js中,才给初学者造成了vue-cli可以解决跨域的错觉。
与在webpack.config.js中配置跨域一样,在vue.config.js中的devServer:{proxy:{}}字段可以编写跨域配置。
具体的配置写法webpack文档写的很清楚。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vue vue
Projects
None yet
Development

No branches or pull requests

6 participants