欢迎来到山村网

vue-cli webpack 开发环境跨域问题

2019-03-09 12:41:49浏览:297 来源:山村网   
核心摘要:edit dev.proxyTable option in config/index.js. The dev server is using http-proxy-middleware for proxying为了解决跨域问

edit dev.proxyTable option in config/index.js. The dev server is using http-proxy-middleware for proxying

为了解决跨域问题,

    通常会使用Jsonp,但是jsonp只能是get请求。 或者使用CORS支持,设置Access-Control-Allow-Origin: *

0 前置技能

熟悉vue-loader 和 webpack

1 基本配置

编辑confix/index.js文件 dev server使用的是http-proxy-middleware来代理

// config/index.jsmodule.exports = { // ... dev: {  proxyTable: {   // proxy all requests starting with /api to jsonplaceholder   '/api': {    target: 'http://jsonplaceholder.typicode.com',    changeOrigin: true,    pathRewrite: {     '^/api': ''    }   }  } }}

The above example will proxy the request /api/posts/1 to http://jsonplaceholder.typicode.com/posts/1.

2 全局匹配

you can provide a filter option that can be a custom function to determine whether a request should be proxied:

提供一个过滤器,制定路由规则和路由方法。

proxyTable: { '*': {  target: 'http://jsonplaceholder.typicode.com',  filter: function (pathname, req) {   return pathname.match('^/api') && req.method === 'GET'  } }}
(责任编辑:豆豆)
下一篇:

刀片服务器没落吸引力原因

上一篇:

何处下载CentOS的Linux内核的源代码?

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com