2018-06-18

import axios from 'axios'

axios.defaults.headers.post['Content-Type'] = 'application/x-www-from-urlencoded';
axios.defaults.baseURL = 'http://note-server.hunger-valley.com'


export default function request(url,type='GET',data={}){
    return new Promise((resolve,reject)=>{
        let option = {
            url,
            method:type,
            validateStatus(status){
                return (status>=200 && status<300) || status === 400;
            }
        }
        if(type.toLowerCase()==='get'){
            option.params = data;
        }else{
            option.data = data;
        }

        axios(option).then(res=>{
            if(res.status===200){
                reslove(res.data)
            }else{
                reject(res.data)
            }
        }).catch(err=>{
            console.log('网络异常')
        })

    })
}


login

    import request from '@/helper/request'
    request('/auth/login','POST',{username:'hunger',password:123456}).then(data=>{
        console.log(data)
    })

你可能感兴趣的:(2018-06-18)