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”,});});}/*...
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/note−comment",noteId:"ijumm32SW4Dwv6rfdmk").then(res=>if(res.status=="10000")console.log(res);elseconsole.log("失败");)//post方法this.post("/api/account/getAccountInfo", {
userId: “CrCubJtbShINpbPYPSg”
}).then(res => {
if (res.status == “10000”) {
console.log(res);
} else {
console.log(“失败”);
}
});
更多推荐
所有评论(0)