当游览器报这样的错时,表示你的请求须要跨域!html
这里,我说的是使用webpack+vue-cli+vue-resource中跨域问题,vue
在config文件下面有index.js文件里有一个叫proxyTable的配置参数webpack
proxyTable: {
'/restful':{
target:'http://xxxxx/member/service/',
changeOrigin:true,
pathRewrite:{//能够不写
'^/restful':'/restful'
}
}
},
changeOrigin这参数设置为true的时候,就能够虚拟一个本地代理的服务接收请求这样就能够解决跨域问题了target是你请求接口的域名git
接口调用的时候能够这么写github
this.$http.post(commonUrl + "/restful/member?op=getMember&access_token=111", { op: 'getMember', }).then(response => }, response => { }); },
有关于API proxy的说明,使用的就是这个参数。
https://vuejs-templates.github.io/webpack/proxy.htmlweb
这个参数主要是一个地址映射表,你能够经过设置将复杂的url简化,例如咱们要请求的地址是api.xxxxxxxx.com/list/1,能够按照以下设置:vue-cli
proxyTable: {
'/list': {
target: 'http://api.xxxxxxxx.com',
pathRewrite: { //能够不写
'^/list': '/list'
}
}
}
这样咱们在写url的时候,只用写成 /list/1 就能够表明api.xxxxxxxx.com/list/1.
那么又是如何解决跨域问题的呢?其实在上面的'list'的参数里有一个changeOrigin参数,接收一个布尔值,若是设置为true,那么本地会虚拟一个服务端接收你的请求并代你发送该请求,这样就不会有跨域问题了,固然这只适用于开发环境。增长的代码以下所示:api
proxyTable: {
'/list': {
target: 'http://api.xxxxxxxx.com',
changeOrigin: true,
pathRewrite: {
'^/list': '/list'
}
}
}