react封装ajax

1.在公共common中创建config.js

// 配置环境dev,test,prod

const env = 'prod';

const evnObj = {

    dev: '',

    test: '',

    prod: 'http://132.232.94.151:7001'

}

export const baseUrl =  evnObj[env];

export default {

    projectName: 'maizuo'

}

2.在common中创建untils.js

import { baseUrl } from './config';

// import obj from './config';

// console.log(baseUrl);

// console.log(obj.projectName);

import axios from 'axios';

export const ajax = (url,data,succ) => {

    url = baseUrl + url;

    data = data || {};

    axios.get(url,{params: data}).then(res=>{

        if (res.data.status === 0) {

            succ(res.data.data);

        } else {

            alert(res.data.msg);

        }

    }).catch(()=> {

        alert('网络请求失败')

    })

}

export const formatDate = (date) => {

    return ''

}

你可能感兴趣的:(react封装ajax)