vue全局封装ajax请求

1、新建一个文件夹,里面放一个api.js文件,里面写上
/* 封装get方法*/
export function get(url, params) {
return KaTeX parse error: Expected '}', got 'EOF' at end of input: …常 this.router.push({
path: “/404”,
});
});
}

/* 封装post方法 */
export function post(url, params) {
return KaTeX parse error: Expected '}', got 'EOF' at end of input: …常 this.router.push({
path: “/404”,
});
});
}
2、然后在main.js里面配置
import {get,post} from ‘./api/api’;
Vue.prototype. g e t = g e t ; V u e . p r o t o t y p e . get=get; Vue.prototype. get=get;Vue.prototype.post = post;
3、在config的index.js里面配置
proxyTable: {
“/api”: {
target: “https://cepingtest.nnyun.net/”, // 对应写上您要发送的接口地址
pathRewrite: { “^/api”: “” },
changeOrigin: true,
secure: false,
headers: {
Referer: “https://cepingtest.nnyun.net/
}
}
},
在这里插入图片描述
4、页面直接引用
get方法
this. g e t ( " / a p i / g e t / n o t e / n o t e − c o m m e n t " , n o t e I d : " i j u m m 32 S W 4 D w v 6 r f d m k " ) . t h e n ( r e s = > i f ( r e s . s t a t u s = = " 10000 " ) c o n s o l e . l o g ( r e s ) ; e l s e c o n s o l e . l o g ( " 失 败 " ) ; ) / / p o s t 方 法 t h i s . get("/api/get/note/note-comment", { noteId: "ijumm32SW4Dwv6rfdmk" }) .then(res => { if (res.status == "10000") { console.log(res); } else { console.log("失败"); } }) // post方法 this. get("/api/get/note/notecomment",noteId:"ijumm32SW4Dwv6rfdmk").then(res=>if(res.status=="10000")console.log(res);elseconsole.log("");)//postthis.post("/api/account/getAccountInfo", {
userId: “CrCubJtbShINpbPYPSg”
}).then(res => {
if (res.status == “10000”) {
console.log(res);
} else {
console.log(“失败”);
}
});

以下是一个基于Axios、Vue和TypeScript的封装Ajax请求示例: ```typescript import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; import { Vue } from 'vue-property-decorator'; // 定义全局配置 axios.defaults.baseURL = 'https://api.example.com'; axios.defaults.timeout = 5000; // 定义请求拦截器 axios.interceptors.request.use((config: AxiosRequestConfig) => { // 在请求发送前做一些处理,例如添加token等 const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }, (error) => { return Promise.reject(error); }); // 定义响应拦截器 axios.interceptors.response.use((response: AxiosResponse) => { // 在响应返回后做一些处理,例如判断是否登录失效等 const data = response.data; if (data.code === 401) { Vue.prototype.$message.error('登录失效,请重新登录!'); localStorage.removeItem('token'); location.href = '/login'; } return response; }, (error) => { return Promise.reject(error); }); // 定义请求方法 export const ajax = { get<T>(url: string, params?: any): Promise<T> { return axios.get(url, { params }).then((res: AxiosResponse) => res.data); }, post<T>(url: string, data?: any): Promise<T> { return axios.post(url, data).then((res: AxiosResponse) => res.data); }, put<T>(url: string, data?: any): Promise<T> { return axios.put(url, data).then((res: AxiosResponse) => res.data); }, delete<T>(url: string, params?: any): Promise<T> { return axios.delete(url, { params }).then((res: AxiosResponse) => res.data); } }; ``` 使用时可以直接引入`ajax`对象调用相应的请求方法: ```typescript import { ajax } from './ajax'; // 发送GET请求 ajax.get('/users', { page: 1, limit: 10 }).then((data) => { console.log(data); }).catch((error) => { console.error(error); }); // 发送POST请求 ajax.post('/users', { name: '张三', age: 18 }).then((data) => { console.log(data); }).catch((error) => { console.error(error); }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值