1.vue-resource
vue中get请求
function getRequest(url, params) {
return new Promise((resolve, reject) => {
Vue.$http.get(
url,
{
params: params
},
{emulateJSON: true}
)
.then((res) => {
resolve(res);
})
.catch((res) => {
reject(res);
});
});
}
post get put delete一样 请求方式不一样而已
2.axios
axios({
method: 'post',
url: '/abc/login',
data: {
userName: 'Lan',
password: '123'
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
3.fetch (API 偏底层,需要封装;默认不带Cookie,需要手动添加; 浏览器支持情况不是很友好,需要第三方的ployfill)
fetch('http://www.mozotech.cn/bangbang/index/user/login', {
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams([
["username", "Lan"],["password", "123456"]
]).toString()
})
.then(res => {
console.log(res);
return res.text();
})
.then(data => {
console.log(data);
})