vue+elemenui 跨域上传图片

.vue 文件

主要注意这两个:
action 请求的地址。直接请求接口https://… 由于同源策略会出现跨域问题,需要后面配置代理,使本地可以跨域请求接口。
name 表单name值


	
	        
	        
	



解决跨域: proxyTable接口代理:
举例:需要请求的接口为:https://abcd.io/data/ocr/xxx.json?accountNo=xxx&password=xxx
代理后:http://localhost:8080/img/xxx.json?accountNo=xxx&password=xxx
onProxyReq: function (proxyReq, req, res) {
//可以通过onProxyReq打印出来看路径的区别
console.log("原路径:" + req.originalUrl, "代理路径:" + req.path)
}

配置文件:config>index.js

dev: {
    // Paths
    assetsSubDirectory: "static",
    assetsPublicPath: "/",
    proxyTable: {
      "/api": { //代理后端的接口
        target: "http://192.168.160.149:8081/", // 后端接口
    	changeOrigin: true,
        pathRewrite: {
          "/api": ""
        }
      },
      '/img': {//代理请求图片的接口
        changeOrigin: true,
        secure: false, //https请求需设置此项
        target: 'https://abcd.io/data/ocr',
        pathRewrite: {
          '^/img': ''  
        }
      }
    },

你可能感兴趣的:(vue,前端,elementui,跨域)