跨域:Response to preflight request doesn t pass access control check: No Access-Control-Allow-Origin

跨域:Response to preflight request doesn t pass access control check: No Access-Control-Allow-Origin
强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

第一、找到config/index.js

  • 设置proxyTable,实际上就是设置代理路径(PS:设置config文件之后,需要重新npm run dev)
dev: {
    env: require('./dev.env'),
    port: 8080,
    autoOpenBrowser: true,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
         '/api': {
              target: '请求地址ip加端口',
              changeOrigin: true,
              pathRewrite: {
                '^/api': '/' 
              }
            }
    },
  }

第二,新建一个封装axios的文件,如 src/api/index.js,里面就是封装的接口方法

//输出通用axios实例
import axios from 'axios';
 
const instance = axios.create({
  timeout: 10000,
   headers: {
    'Content-Type': "application/json;charset=utf-8"
  }
});
 
export default {
  regist (data){
      return instance.post('/api/createOrUpdateUser',data)
  }
};

第三、在main.js引用

import instance_ from './api/index';
Vue.prototype.instance = instance_  //axios实例

第四,就可以调用自己定义的接口了,如           

this.instance.regist({             
     userId: '',       
     userName: this.username,        
 }).then(res=>{
     console.log("sd")
 }).catch(function (error) {
     console.log(error);
 });

 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/18566

发表评论