|
@@ -0,0 +1,56 @@
|
|
|
|
+/**
|
|
|
|
+ * axios封装
|
|
|
|
+ * @param {*} param0
|
|
|
|
+ * @returns
|
|
|
|
+ */
|
|
|
|
+function axios({method,url,params,data}){
|
|
|
|
+ method = method.toUpperCase()
|
|
|
|
+ return new Promise((resolve,reject)=>{
|
|
|
|
+ const xhr = new XMLHttpRequest()
|
|
|
|
+ let str = ''
|
|
|
|
+ for(let k in params){
|
|
|
|
+ str += `${k}=${params[k]}&`
|
|
|
|
+ }
|
|
|
|
+ str = str.slice(0,-1)
|
|
|
|
+ xhr.open(method,url+'?'+str)
|
|
|
|
+ if(['POST','PUT','DELETE'].includes(method)){
|
|
|
|
+ xhr.setRequestHeader(
|
|
|
|
+ 'Content-type','application/json'
|
|
|
|
+ )
|
|
|
|
+ xhr.send(JSON.stringify(data))
|
|
|
|
+ }else{
|
|
|
|
+ xhr.send()
|
|
|
|
+ }
|
|
|
|
+ xhr.responseType = 'json'
|
|
|
|
+ xhr.onreadystatechange = function(){
|
|
|
|
+ if(xhr.readyState === 4){
|
|
|
|
+ if(xhr.status >= 200 && xhr.status < 300){
|
|
|
|
+ resolve({
|
|
|
|
+ status:xhr.status,
|
|
|
|
+ message:xhr.statusText,
|
|
|
|
+ body:xhr.response
|
|
|
|
+ })
|
|
|
|
+ }else{
|
|
|
|
+ reject(new Error('请求失败,状态码:'+xhr.status))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//添加方法
|
|
|
|
+axios.get = function(url,options){
|
|
|
|
+ return axios(Object.assign(options,{method:'GET',url:url}))
|
|
|
|
+}
|
|
|
|
+//添加方法
|
|
|
|
+axios.post = function(url,options){
|
|
|
|
+ return axios(Object.assign(options,{method:'POST',url:url}))
|
|
|
|
+}
|
|
|
|
+//添加方法
|
|
|
|
+axios.put = function(url,options){
|
|
|
|
+ return axios(Object.assign(options,{method:'PUT',url:url}))
|
|
|
|
+}
|
|
|
|
+//添加方法
|
|
|
|
+axios.delete = function(url,options){
|
|
|
|
+ return axios(Object.assign(options,{method:'DELETE',url:url}))
|
|
|
|
+}
|