P粉4587250402023-08-21 10:51:25
我認為你的問題是jsfiddle
只能處理form-urlencoded
請求。但是正確的方法是將正確的json
作為請求體傳遞:
fetch('https://httpbin.org/post', { method: 'POST', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: JSON.stringify({a: 7, str: 'Some string: &=&'}) }).then(res => res.json()) .then(res => console.log(res));
P粉8199374862023-08-21 10:15:17
使用ES2017的async/await
支援,這是如何進行POST
JSON資料的方法:
(async () => { const rawResponse = await fetch('https://httpbin.org/post', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({a: 1, b: 'Textual content'}) }); const content = await rawResponse.json(); console.log(content); })();