解讀請求方式Method和請求類型Content-Type
更新時間:2024年09月18日 10:19:24 作者:勿語&
HTTP請求中,Content-Type頭部用于指定請求體或響應(yīng)體的類型,常見的有application/x-www-form-urlencoded、multipart/form-data、application/json、text/plain、application/xml等,常用請求方式包括Get、Post、Put、Delete
1. 請求Content-Type (用來指定請求體或響應(yīng)體的類型)
- application/x-www-form-urlencoded:參數(shù)以鍵值對形式傳遞,適合普通表單提交。(常用)
- multipart/form-data:用于文件上傳,也可以包含其他鍵值對。(常用)
- application/json:用于發(fā)送JSON格式的數(shù)據(jù),廣泛應(yīng)用于API交互。(常用)
- text/plain:用于發(fā)送純文本數(shù)據(jù)。
- application/xml 或 text/xml:用于發(fā)送XML格式的數(shù)據(jù)。
2. 常用請求方式
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請求 fetch(url) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error));
Post:常用于新增,請求參數(shù)放在請求體中
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ā)送請求 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會自動設(shè)置 }) .then(response => response.json()) .then(data => console.log(data));
Put: 常用于修改,請求參數(shù)放在請求體中
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: 常用于刪除,請求參數(shù)放在請求體中或URL中
// 刪除單條記錄時 fetch('/api/users/1', { method: 'DELETE' }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); // 刪除多條記錄時 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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript創(chuàng)建對象的七種經(jīng)典方式分享
JavaScript 創(chuàng)建對象的方式有很多,通過 Object 構(gòu)造函數(shù)或?qū)ο笞置媪康姆绞揭部梢詣?chuàng)建單個對象,顯然這兩種方式會產(chǎn)生大量的重復(fù)代碼,并不適合量產(chǎn)。本文介紹了七種非常經(jīng)典的創(chuàng)建對象的方式,希望對大家有所幫助2022-11-11A標簽觸發(fā)onclick事件而不跳轉(zhuǎn)的多種解決方法
一個標簽僅僅是要觸發(fā)onclick行為,遇到了A標簽觸發(fā)onclick事件時不執(zhí)行跳轉(zhuǎn),下面與大家分享下四種解決方法,感興趣的朋友可以參考下哈2013-06-06