vote life iconVote Life: /

DevNotes - JavaScript

Fetch API

2022-04-11

Fetch API - Web API | MDN

1.GET (JSON変換)

async function fetch(url) { try { const res = await fetch(url); if (res.status === 200) { const json = await res.json(); text = json["text"]; number = Number(json["number"]); } catch (error) {} };

2.POST

async function fetch(url) { try { const res = await fetch(url, {method: "POST"}); } catch (error) {} };

3.POST (JSON変換)

var text = "text"; var number = 0; const jsonData = {text:text,number:number}; const form = new FormData(); form.append("postData", JSON.stringify(jsonData)); async function fetch(url, form) { try { const res = await fetch(url, { method: "POST", body: form }); if (res.status === 200) { const json = await res.json(); text = json["text"]; number = Number(json["number"]); } catch (error) {} };