uniapp跨域问题

跨域问题

方式一:添加vue.config.js(最常用)

module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:8877', // 后端服务器的地址
        changeOrigin: true, // 是否改变原始主机头为目标URL
        ws: true, // 是否代理websockets
        pathRewrite: {'^/api': ''} // 重写路径,去掉'/api'前缀
      }
    }
  }
}

方式二:在manifest.json中添加

"h5": {
                "devServer": {
                  "proxy": {
                    "/api": {
                      "target": "http://localhost:8877", // 后端服务地址
                      "changeOrigin": true,
                      "pathRewrite": {
                        "^/api": "" 
                      }
                    }
                  }
                }
              }

方式三:
服务器添加

router.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*"); // 允许所有域名访问
  res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容