解讀請(qǐng)求方式Method和請(qǐng)求類(lèi)型Content-Type
1. 請(qǐng)求Content-Type (用來(lái)指定請(qǐng)求體或響應(yīng)體的類(lèi)型)
- 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:常用于查詢(xún),一般拼接在url后面
如:http://example.com/api/resource?key1=value1&key2=value2
// 構(gòu)造查詢(xún)字符串,形如: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)文章
JavaScript求解最長(zhǎng)回文子串的方法分享
這篇文章主要為大家介紹了JavaScript求解最長(zhǎng)回文子串的幾種方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-08-08
three.js實(shí)現(xiàn)3D影院的原理的代碼分析
本篇文章主要給大家講解了如何通過(guò)three.js實(shí)現(xiàn)3D影院的功能以及原理分析,需要的朋友參考一下吧。2017-12-12
利用JS定時(shí)器實(shí)現(xiàn)元素移動(dòng)
這篇文章主要為大家詳細(xì)介紹了利用JS定時(shí)器實(shí)現(xiàn)元素移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
JavaScript創(chuàng)建對(duì)象的七種經(jīng)典方式分享
JavaScript 創(chuàng)建對(duì)象的方式有很多,通過(guò) Object 構(gòu)造函數(shù)或?qū)ο笞置媪康姆绞揭部梢詣?chuàng)建單個(gè)對(duì)象,顯然這兩種方式會(huì)產(chǎn)生大量的重復(fù)代碼,并不適合量產(chǎn)。本文介紹了七種非常經(jīng)典的創(chuàng)建對(duì)象的方式,希望對(duì)大家有所幫助2022-11-11
詳解JavaScript原型對(duì)象的this指向問(wèn)題
這篇文章主要為大家介紹了JavaScript原型對(duì)象的this指向問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-11-11
A標(biāo)簽觸發(fā)onclick事件而不跳轉(zhuǎn)的多種解決方法
一個(gè)標(biāo)簽僅僅是要觸發(fā)onclick行為,遇到了A標(biāo)簽觸發(fā)onclick事件時(shí)不執(zhí)行跳轉(zhuǎn),下面與大家分享下四種解決方法,感興趣的朋友可以參考下哈2013-06-06

