解讀請(qǐng)求方式Method和請(qǐng)求類型Content-Type
1. 請(qǐng)求Content-Type (用來指定請(qǐng)求體或響應(yīng)體的類型)
- application/x-www-form-urlencoded:參數(shù)以鍵值對(duì)形式傳遞,適合普通表單提交。(常用)
- multipart/form-data:用于文件上傳,也可以包含其他鍵值對(duì)。(常用)
- application/json:用于發(fā)送JSON格式的數(shù)據(jù),廣泛應(yīng)用于API交互。(常用)
- text/plain:用于發(fā)送純文本數(shù)據(jù)。
- application/xml 或 text/xml:用于發(fā)送XML格式的數(shù)據(jù)。
2. 常用請(qǐng)求方式
Get:常用于查詢,一般拼接在url后面
如:http://example.com/api/resource?key1=value1&key2=value2
// 構(gòu)造查詢字符串,形如:key1=value1&key2=value2
const queryParams = new URLSearchParams();
queryParams.append('key1', 'value1');
queryParams.append('key2', 'value2');
// 構(gòu)造完整的URL
const url = `http://example.com/api/resource?${queryParams.toString()}`;
// 發(fā)送GET請(qǐng)求
fetch(url)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));Post:常用于新增,請(qǐng)求參數(shù)放在請(qǐng)求體中
Content-Type = ‘application/json’
const user = {
id: 1,
name: 'John Doe',
email: 'john.doe@example.com'
};
fetch('/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(user)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));Content-Type = ‘application/x-www-form-urlencoded’
const queryParams = new URLSearchParams();
queryParams.append('key1', 'value1');
queryParams.append('key2', 'value2');
fetch('/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: queryParams.toString()
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));Content-Type = ‘multipart/form-data’
// JavaScript 發(fā)送請(qǐng)求
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('description', 'This is a test file.');
fetch('/upload', {
method: 'POST',
body: formData // 不需要設(shè)置Content-Type,F(xiàn)ormData會(huì)自動(dòng)設(shè)置
})
.then(response => response.json())
.then(data => console.log(data));Put: 常用于修改,請(qǐng)求參數(shù)放在請(qǐng)求體中
const user = {
id: 1,
name: 'John Doe',
email: 'john.doe@example.com'
};
fetch('/api/users', {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(user)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));Delete: 常用于刪除,請(qǐng)求參數(shù)放在請(qǐng)求體中或URL中
// 刪除單條記錄時(shí)
fetch('/api/users/1', {
method: 'DELETE'
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
// 刪除多條記錄時(shí)
const ids=[1,2,3,4]
fetch('/api/users', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(ids)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)仿PS的調(diào)色板效果完整實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)仿PS的調(diào)色板效果,結(jié)合完整實(shí)例形式分析了javascript通過運(yùn)算與動(dòng)態(tài)操作頁(yè)面元素實(shí)現(xiàn)調(diào)色板功能的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
js+css使DIV始終居于屏幕中間 左下 左上 右上 右下的代碼集合
js+css使DIV始終居于屏幕中間 左下 左上 右上 右下的代碼集合,需要的朋友可以參考下。2011-03-03
js強(qiáng)制把網(wǎng)址設(shè)為默認(rèn)首頁(yè)
有時(shí)候你會(huì)發(fā)現(xiàn)設(shè)首頁(yè)為失效,那么來一個(gè)js強(qiáng)制設(shè)置首頁(yè)的代碼,不過為了綠色上網(wǎng),盡量不要強(qiáng)迫你的用戶意志,弄不好網(wǎng)站用戶體驗(yàn)會(huì)降低。感興趣的小伙伴可以參考一下2015-09-09
兩種JS實(shí)現(xiàn)屏蔽鼠標(biāo)右鍵的方法
這篇文章主要介紹了兩種JS實(shí)現(xiàn)屏蔽鼠標(biāo)右鍵的方法,瀏覽者在訪問你網(wǎng)頁(yè)的時(shí)候就不能點(diǎn)擊右鍵,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08
JS彈出可拖拽可關(guān)閉的div層完整實(shí)例
這篇文章主要介紹了JS彈出可拖拽可關(guān)閉的div層完整實(shí)現(xiàn)方法,包括對(duì)div彈出層的樣式及功能的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02

