import qs
from "qs"
export default function ajax(url
,params
={},type
='GET')
{
if(type
=='GET')
{
let str
='';
Object
.keys(params
).forEach((item
,index
)=>{
str
+=item
+'='+params
[item
]+'&';
})
if(str
)
{
str
=str
.substring(0,str
.length
-1);
str
='?'+str
;
}
return fetch(url
+str
).then(data
=>data
.json())
}else{
return fetch(url
,{
method
:'POST',
headers
:{
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded'
},
body
:qs
.stringify(params
)
}).then(data
=>data
.json())
}
}
转载请注明原文地址:https://tech.qufami.com/read-28832.html