- 封装axios函数
//能异步发送ajax请求
import axios from 'axios'
import qs from 'qs'
import { message } from 'antd';
export default function ajax(url,data={},type){
let promise
return new Promise((resolve,reject)=>{
if(type==='POST'){
promise= axios.post(url,qs.stringify(data))
}else{
promise= axios.get(url,{
params:qs.stringify(data)
})
}
promise.then(res=>{
resolve(res)
}).catch(error=>{
message.error('请求出错'+error.message)
})
})
}
调用登录接口
import ajax from './api.js'
// export function login(username,password){
// return ajax('goods/loginTxt.php',{username,password},'POST')
// }
//箭头有返回的作用,有箭头加{}需要return
//export const reqLogin =(username,password)=>{return ajax('goods/loginTxt.php',{username,password},'POST')}
export const reqLogin =(username,password)=>ajax('/goods/loginTxt.php',{username,password},'POST')