{"id":3010,"date":"2024-12-29T10:40:22","date_gmt":"2024-12-29T01:40:22","guid":{"rendered":"https:\/\/www.agile-software.site\/?p=3010"},"modified":"2024-12-29T10:40:23","modified_gmt":"2024-12-29T01:40:23","slug":"fetch%e3%81%a8%e3%81%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/agile-software.net\/?p=3010","title":{"rendered":"fetch\u3068\u306f\uff1f"},"content":{"rendered":"\n<p><code>fetch<\/code>\u306f\u3001Web API\u306e\u4e00\u90e8\u3068\u3057\u3066\u63d0\u4f9b\u3055\u308c\u308b\u30d6\u30e9\u30a6\u30b6\u304a\u3088\u3073Node.js\u74b0\u5883\u3067\u4f7f\u7528\u3055\u308c\u308bAPI\u3067\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<code>fetch<\/code>\u306e\u57fa\u672c\u7684\u306a\u4ed5\u7d44\u307f\u3092\u4ee5\u4e0b\u306b\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u672c\u69cb\u9020<\/h3>\n\n\n\n<p><code>fetch<\/code>\u306f\u3001\u30ea\u30bd\u30fc\u30b9\uff08\u901a\u5e38\u306fHTTP\u30ea\u30af\u30a8\u30b9\u30c8\uff09\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3042\u308a\u3001<code>Promise<\/code>\u3092\u8fd4\u3057\u307e\u3059\u3002\u57fa\u672c\u7684\u306a\u4f7f\u7528\u65b9\u6cd5\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>\nfetch(&#39;https:\/\/api.example.com\/data&#39;)\n  .then(response =&gt; response.json())\n  .then(data =&gt; console.log(data))\n  .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/code><\/pre><\/div>\n\n\n\n<p><br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30b9\u30c6\u30c3\u30d7\u3054\u3068\u306e\u8aac\u660e<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u9001\u4fe1<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>fetch<\/code>\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u6307\u5b9a\u3055\u308c\u305fURL\u306bHTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3001<code>GET<\/code>\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u9001\u4fe1\u3055\u308c\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u53d7\u4fe1<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>fetch<\/code>\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u30b5\u30fc\u30d0\u30fc\u304b\u3089\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d6\u5f97\u3057\u3001<code>Promise<\/code>\u3092\u8fd4\u3057\u307e\u3059\u3002\u3053\u306e<code>Promise<\/code>\u306f\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u53d7\u4fe1\u3055\u308c\u308b\u3068\u89e3\u6c7a\u3055\u308c\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u51e6\u7406<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>Promise<\/code>\u306e<code>then<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u51e6\u7406\u3057\u307e\u3059\u3002\u30ec\u30b9\u30dd\u30f3\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u306f\u3001HTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9\u3084\u30d8\u30c3\u30c0\u30fc\u60c5\u5831\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u306e\u5909\u63db<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30dc\u30c7\u30a3\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u3001<code>.json()<\/code>\u30e1\u30bd\u30c3\u30c9\u3084<code>.text()<\/code>\u30e1\u30bd\u30c3\u30c9\u306a\u3069\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u3092\u5909\u63db\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001<code>.json()<\/code>\u30e1\u30bd\u30c3\u30c9\u306f\u30ec\u30b9\u30dd\u30f3\u30b9\u3092JSON\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30a8\u30e9\u30fc\u3084\u30b5\u30fc\u30d0\u30fc\u30a8\u30e9\u30fc\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306b\u3001<code>catch<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3092\u884c\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">HTTP\u30e1\u30bd\u30c3\u30c9\u306e\u4f7f\u7528<\/h3>\n\n\n\n<p><code>fetch<\/code>\u306f\u3001<code>GET<\/code>\u30ea\u30af\u30a8\u30b9\u30c8\u3060\u3051\u3067\u306a\u304f\u3001\u4ed6\u306eHTTP\u30e1\u30bd\u30c3\u30c9\uff08<code>POST<\/code>, <code>PUT<\/code>, <code>DELETE<\/code>\u306a\u3069\uff09\u3082\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306f<code>POST<\/code>\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>\nfetch(&#39;https:\/\/api.example.com\/data&#39;, {\n  method: &#39;POST&#39;,\n  headers: {\n    &#39;Content-Type&#39;: &#39;application\/json&#39;\n  },\n  body: JSON.stringify({ key: &#39;value&#39; })\n})\n  .then(response =&gt; response.json())\n  .then(data =&gt; console.log(data))\n  .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/code><\/pre><\/div>\n\n\n\n<p><br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30af\u30ed\u30b9\u30aa\u30ea\u30b8\u30f3\u30ea\u30af\u30a8\u30b9\u30c8<\/h3>\n\n\n\n<p><code>fetch<\/code>\u306f\u30af\u30ed\u30b9\u30aa\u30ea\u30b8\u30f3\u30ea\u30af\u30a8\u30b9\u30c8\uff08CORS\uff09\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u304a\u308a\u3001\u7570\u306a\u308b\u30c9\u30e1\u30a4\u30f3\u304b\u3089\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002CORS\u30dd\u30ea\u30b7\u30fc\u304c\u9069\u5207\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30ad\u30e3\u30f3\u30bb\u30eb\u53ef\u80fd\u306a\u30ea\u30af\u30a8\u30b9\u30c8<\/h3>\n\n\n\n<p><code>fetch<\/code>\u306f\u3001<code>AbortController<\/code>\u3092\u4f7f\u7528\u3057\u3066\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>\nconst controller = new AbortController();\nconst signal = controller.signal;\n\nfetch(&#39;https:\/\/api.example.com\/data&#39;, { signal })\n  .then(response =&gt; response.json())\n  .then(data =&gt; console.log(data))\n  .catch(error =&gt; console.error(&#39;Error:&#39;, error));\n\n\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3059\u308b\ncontroller.abort();<\/code><\/pre><\/div>\n\n\n\n<p><br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u307e\u3068\u3081<\/h3>\n\n\n\n<p><code>fetch<\/code>\u306f\u5f37\u529b\u304b\u3064\u67d4\u8edf\u306aAPI\u3067\u3042\u308a\u3001\u30d6\u30e9\u30a6\u30b6\u74b0\u5883\u3068Node.js\u74b0\u5883\u306e\u4e21\u65b9\u3067\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002\u30b7\u30f3\u30d7\u30eb\u306a\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3068Promise\u30d9\u30fc\u30b9\u306e\u8a2d\u8a08\u306b\u3088\u308a\u3001\u975e\u540c\u671f\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u7c21\u5358\u306b\u51e6\u7406\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>fetch\u306f\u3001Web API\u306e\u4e00\u90e8\u3068\u3057\u3066\u63d0\u4f9b\u3055\u308c\u308b\u30d6\u30e9\u30a6\u30b6\u304a\u3088\u3073Node.js\u74b0\u5883\u3067\u4f7f\u7528\u3055\u308c\u308bAPI\u3067\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3046\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002fetch\u306e\u57fa\u672c\u7684\u306a\u4ed5\u7d44\u307f\u3092\u4ee5\u4e0b\u306b\u8aac\u660e\u3057\u307e\u3059\u3002 \u57fa\u672c\u69cb\u9020 fetch<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jin_ogp_image_url":"","_jin_last_featured_id":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3010","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/3010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3010"}],"version-history":[{"count":1,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/3010\/revisions"}],"predecessor-version":[{"id":3011,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/3010\/revisions\/3011"}],"wp:attachment":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}