- get請求
fetch('https://mywebsite.com/endpoint/', {
method: 'GET', // 請求方式 有GET POST
headers: {
'Accept': 'application/json', //希望接收的格式
'Content-Type': 'application/json', //發(fā)送的格式 一般都為JSON
},
})
.then((response) => response.json()) // 接收到數(shù)據(jù)后 序列號 也可text text既沒有格式
.catch((error) => {
console.error(error);
}); //請求失敗
get 請求一般就是 在一個域名 + 請求action + ? + 參數(shù) 參數(shù)之間用&連接
ex:
https://huodong.taobao.com/wow/tb-act-new/ (*** action -->) act/meiyan?spm=a21bo.2017.201862-1.d1.67aef70eSbQFBz&pos=1&acm=20140506001.1003.2.2546600&scm=1003.2.20140506001.OTHER_1510588319083_2546600
2.post 請求
fetch('https://mywebsite.com/endpoint/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'yourValue', //參數(shù)放到body中不拼接在網(wǎng)址后 轉(zhuǎn)換為JSON格式
secondParam: 'yourOtherValue',
})
})
.then((response) => response.json()) // 接收到數(shù)據(jù)后 序列號 也可text text既沒有格式
.catch((error) => {
console.error(error);
}); //請求失敗
post 請求不同于get 一般情況參數(shù)不拼接在 網(wǎng)址后面
我們將參數(shù) 轉(zhuǎn)換為JSON格式 放到body中 反序列化