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.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/
}
}
},
vue全局封装ajax请求
4、页面直接引用
get方法
this.get("/api/get/note/notecomment",noteId:"ijumm32SW4Dwv6rfdmk").then(res=>if(res.status=="10000")console.log(res);elseconsole.log("");)//postthis.get("/api/get/note/note-comment", { noteId: "ijumm32SW4Dwv6rfdmk" }) .then(res => { if (res.status == "10000") { console.log(res); } else { console.log("失败"); } }) // post方法 this.post("/api/account/getAccountInfo", {
userId: “CrCubJtbShINpbPYPSg”
}).then(res => {
if (res.status == “10000”) {
console.log(res);
} else {
console.log(“失败”);
}
});