{"id":5982,"date":"2020-07-18T13:41:58","date_gmt":"2020-07-18T04:41:58","guid":{"rendered":"https:\/\/nsme.red\/wp\/?p=5982"},"modified":"2020-09-30T17:28:25","modified_gmt":"2020-09-30T08:28:25","slug":"fetch-api","status":"publish","type":"post","link":"https:\/\/core.nsme.net\/notes\/vue-js\/fetch-api\/5982\/","title":{"rendered":"fetch API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>IE<\/code>\u3092\u9664\u304f\u307b\u307c\u3059\u3079\u3066\u306e\u30d6\u30e9\u30a6\u30b6\u3067\u6709\u52b9<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\uff08GET\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">fetch('\/fetch?cohai')\n    .then(response =&gt; {\n        return response.json(); \/\/ Promise\u3092\u8fd4\u3059\n    })\n    .then(data =&gt; { \/\/  JSON\u30c7\u30fc\u30bf\n        console.log(data);\n    })\n    .catch(error =&gt; { \/\/  \u30a8\u30e9\u30fc\u306e\u5834\u5408\n        console.log(error);\n    });<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/text\u3068\u3057\u3066\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\n<code>fetch(url)\n\t.then(response =&gt; {\n\t\treturn response.text();\n\t})\n\t.then(text =&gt; {\n\t\tconsole.log(text);\n\t\tthis.name = text;\n\t});<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7701\u7565\u5f62\n fetch(url)\n  .then(res=> res.text() )\n  .then( text=> console.log(text) );<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u9001\u4fe1\u3059\u308b\uff08POST, PUT, DELETE\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">exec: function(){\nlet data = {name:this.name, age:this.age};\nfetch(url, {\nmethod: 'POST',\nbody: JSON.stringify(data),\nheaders: {\n\"Content-Type\": \"application\/json\"\n }\n})\n.then(response =&gt; response.json())\n.then(data =&gt; {\n console.log(data);\n this.lists = data;\n})\n.catch(error =&gt; {\n console.log(error);\n});\n}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u53c2\u8003\n \u3000<a href=\"https:\/\/blog.capilano-fw.com\/?p=6646\" target=\"_blank\" rel=\"noreferrer noopener\">axios\u4e0d\u8981\uff01fetch()\u3067Ajax\u901a\u4fe1\u3059\u308b\u65b9\u6cd5<\/a>\n  <a href=\"https:\/\/noumenon-th.net\/programming\/2019\/01\/16\/fetch01\/\" target=\"_blank\" rel=\"noreferrer noopener\">fetch\u3092\u5229\u7528\u3059\u308b[GET][POST]<\/a><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">PHP<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nswitch ($_SERVER&#91;'REQUEST_METHOD']){\ncase 'GET' :\n\t\t$data = array(\n\t\t'name'=>'mido',\n\t\t'age'=>'40'\n\t);\n\techo json_encode($data);\n\tbreak;\n\t\ncase 'POST' :\n  $params = json_decode(file_get_contents('php:\/\/input'), true);\n\t\/\/\u51e6\u7406\n\techo json_encode($params);\n\tbreak;\n\t\ncase 'PUT' :\n  $params = json_decode(file_get_contents('php:\/\/input'), true);\n\t\/\/\u51e6\u7406\n\techo json_encode($params);\n\tbreak;\n\t\ncase 'DELETE' :\n  $params = json_decode(file_get_contents('php:\/\/input'), true);\n\t\/\/\u51e6\u7406\n\techo json_encode($params);\n\tbreak;\n\t\n}\n?><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>IE\u3092\u9664\u304f\u307b\u307c\u3059\u3079\u3066\u306e\u30d6\u30e9\u30a6\u30b6\u3067\u6709\u52b9 \u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\uff08GET\uff09 fetch(&#8216;\/fetch?cohai&#8217;) .then(response =&gt; { return response.json(); \/\/ Promis [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[36],"tags":[],"class_list":["post-5982","post","type-post","status-publish","format-standard","hentry","category-fetch-api"],"acf":[],"_links":{"self":[{"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/posts\/5982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/comments?post=5982"}],"version-history":[{"count":12,"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/posts\/5982\/revisions"}],"predecessor-version":[{"id":6077,"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/posts\/5982\/revisions\/6077"}],"wp:attachment":[{"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/media?parent=5982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/categories?post=5982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/core.nsme.net\/notes\/wp-json\/wp\/v2\/tags?post=5982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}