从浏览器控制台发送get,post请求
---------------------get请求---------------------------
fetch(url, {
method: 'get',
})
.then(response => response.json())
.then(data => {
// 获取到响应的数据后的处理逻辑
console.log(data);
})
.catch(error => {
// 请求发生错误的处理逻辑
console.log(error);
});
---------------------post请求-------------------------------
fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/json',
// 如果需要在请求头中添加其他自定义参数,可以在这里添加
},
body:JSON.stringify(data) // 请求体中的数据
})
.then(response => response.json())
.then(data => {
// 获取到响应的数据后的处理逻辑
console.log(data);
})
.catch(error => {
// 请求发生错误的处理逻辑
console.log(error);
});