{"id":13633,"date":"2021-06-20T20:57:04","date_gmt":"2021-06-20T11:57:04","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13633"},"modified":"2023-05-27T20:53:29","modified_gmt":"2023-05-27T11:53:29","slug":"%e3%80%90javascript%e3%80%91%e3%80%8capi%e5%ae%9f%e8%a1%8c%e3%83%a9%e3%82%a4%e3%83%96%e3%83%a9%e3%83%aa%e3%80%8d%e3%81%ae%e6%af%94%e8%bc%83","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13633","title":{"rendered":"\u3010JavaScript\u3011\u300cHTTP\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30e9\u30a4\u30d6\u30e9\u30ea\u300d\u306e\u6bd4\u8f03(axios\u3001fetch)\u3001\u5c0e\u5165\u65b9\u6cd5\u306a\u3069"},"content":{"rendered":"<h2>axios<\/h2>\n<ul>\n<li>\u30d6\u30e9\u30a6\u30b6\u3084\u3001Node.js\u3067\u52d5\u304f<span style=\"color: #ff0000;\"><strong>Ajax\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u305f\u3081\u306ePromise\u30d9\u30fc\u30b9\u306eHTTP\u30af\u30e9\u30a4\u30a2\u30f3\u30c8<\/strong><\/span>\u3067\u3059\u3002<\/li>\n<li>404\u306a\u3069\u30a8\u30e9\u30fc\u3092\u53d7\u3051\u53d6\u308b\u3068catch\u3067\u304d\u308b\u3002<\/li>\n<li>GitHub\u30b9\u30bf\u30fc\u6570\u304c\u591a\u3044\u3002<\/li>\n<\/ul>\n<h3>\u30e1\u30ea\u30c3\u30c8<\/h3>\n<ul>\n<li>\u30ec\u30b9\u30dd\u30f3\u30b9\u304cjson\u30d9\u30fc\u30b9\u3067\u8fd4\u3063\u3066\u304f\u308b\u306e\u3067\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u4f7f\u3063\u3066\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3059\u308b\u306e\u304c\u7c21\u5358\u3002\uff08\u4f8b\u3048\u3070\u3001\u30a8\u30e9\u30fc\u30b3\u30fc\u30c9xxx\u306e\u5834\u5408\u306f\u3053\u3046\u3059\u308b\u306a\u3069\u306e\u51e6\u7406\u306e\u5b9f\u88c5\u304c\u697d\uff09<\/li>\n<\/ul>\n<h3>\u7528\u9014<\/h3>\n<p>Vue.js\u3084React\u3067\u3001Ajax\u3092\u3059\u308b\u5834\u5408\u306b\u306f\u3088\u304f\u4f7f\u308f\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<pre class=\"lang:default decode:true \">yarn add axios<\/pre>\n<h3>\u7d44\u307f\u8fbc\u307f<\/h3>\n<pre class=\"lang:default decode:true\">import axios from 'axios'\r\n\r\naxios.get('\/user?ID=12345')\r\n  .then((response) =&gt; {\r\n    \/\/ \u6210\u529f\u3057\u305f\u5834\u5408\r\n    console.log(response);\r\n  })\r\n  .catch((error) =&gt; {\r\n    \/\/ \u5931\u6557\u3057\u305f\u5834\u5408\r\n    console.log(error);\r\n      console.log(error.response.data);\r\n      console.log(error.response.status);\r\n      console.log(error.response.headers);\r\n  })\r\n  .finally(() =&gt; {\r\n    \/\/ \u4e8b\u5f8c\u51e6\u7406\r\n  });<\/pre>\n<p>axios\u306f\u30a8\u30e9\u30fc\u30a8\u30e9\u30fc\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u5185\u5bb9\u3092\u300cerror.response\u300d\u3067\u898b\u308c\u308b\u306e\u3067\u3059\u304c\u304b\u306a\u308a\u8a73\u7d30\u306b\u78ba\u8a8d\u304c\u3067\u304d\u308b\u306e\u3067\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<h2 class=\"p1\">fetch(node-fetch)<\/h2>\n<p>\u30d6\u30e9\u30a6\u30b6\u6a19\u6e96\u306eAPI\u3067\u3059\u3002\uff08node.js\u958b\u767a\u3067\u306fnode-fetch\u3068\u3044\u3046\u3082\u306e\u304c\u4f7f\u3048\u307e\u3059\u3002\uff09<\/p>\n<h3>\u30e1\u30ea\u30c3\u30c8<\/h3>\n<ul>\n<li>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u4e0d\u8981\uff08\u305f\u3060\u3001node.js\u74b0\u5883\u3067\u3042\u308c\u3070node-fetch\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002\uff09<\/li>\n<li>\u30b5\u30a4\u30ba\u304c\u5c0f\u3055\u3044<\/li>\n<li>\u3053\u3061\u3089\u3082Promise\u30d9\u30fc\u30b9\u306e\u30e2\u30c0\u30f3JS\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<li>npm\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u6570\u3067\u8a00\u3048\u3070axios\u3088\u308a\u3082\u591a\u3044\u3002<\/li>\n<\/ul>\n<h3>\u30c7\u30e1\u30ea\u30c3\u30c8<\/h3>\n<ul>\n<li>\u30ec\u30b9\u30dd\u30f3\u30b9\u304cjson\u5f62\u5f0f\u3067\u306f\u306a\u3044\u306e\u3067\u3082\u3057\u30ec\u30b9\u30dd\u30f3\u30b9\u30c7\u30fc\u30bf\u3092\u6574\u5f62\u3057\u305f\u3044\u5834\u5408\u306a\u3069\u306fjson\u30e1\u30bd\u30c3\u30c9\u306a\u3069\u3092\u4f7f\u3063\u305f\u308a\u3057\u3066\u5225\u9014\u6574\u5f62\u304c\u5fc5\u8981\u306b\u306a\u308b\u3002<\/li>\n<\/ul>\n<h3>\u7d44\u307f\u8fbc\u307f<\/h3>\n<pre class=\"lang:default decode:true \">fetch('https:\/\/xxx\/user?ID=12345')\r\n  .then(response =&gt; {\r\n    if (!response.ok) {\r\n      throw new Error(`${response.status} ${response.statusText}`);\r\n    }\r\n    return response.json();\r\n  })\r\n  .then(data =&gt; console.log(data))\r\n  .catch(error =&gt; console.error(error));<\/pre>\n","protected":false},"excerpt":{"rendered":"axios \u30d6\u30e9\u30a6\u30b6\u3084\u3001Node.js\u3067\u52d5\u304fAjax\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u305f\u3081\u306ePromise\u30d9\u30fc\u30b9\u306eHTTP\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3067\u3059\u3002 404\u306a\u3069\u30a8\u30e9\u30fc\u3092\u53d7\u3051\u53d6\u308b\u3068catch\u3067\u304d\u308b\u3002 GitHub\u30b9\u30bf\u30fc\u6570\u304c\u591a\u3044\u3002 \u30e1\u30ea\u30c3\u30c8 \u30ec\u30b9\u30dd\u30f3 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13633"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13633"}],"version-history":[{"count":9,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13633\/revisions"}],"predecessor-version":[{"id":20274,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13633\/revisions\/20274"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13633"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}