{"id":1598,"date":"2024-08-02T10:28:49","date_gmt":"2024-08-02T08:28:49","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1598"},"modified":"2024-08-02T10:28:50","modified_gmt":"2024-08-02T08:28:50","slug":"009-api-rest-crud-con-axios-1","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/16-api-rest\/009-api-rest-crud-con-axios-1\/","title":{"rendered":"009. API REST: CRUD con Axios (1)"},"content":{"rendered":"\n<p>Para este siguiente ejm tenemos que mandar a llamar a la librer\u00eda <strong>Axios<\/strong>, para ello pinchamos en el siguiente enlace:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/axios\/axios\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/axios\/axios<\/a><\/li>\n<\/ul>\n\n\n\n<!--more-->\n\n\n\n<p>En la documentaci\u00f3n viene el <strong>cdn<\/strong> de la \u00faltima versi\u00f3n, el cual ponemos antes del <strong>script<\/strong> donde tenemos toda nuestra programaci\u00f3n. Despu\u00e9s de incluir la librer\u00eda <strong>Axios<\/strong>, comenzamos a escribir nuestros <strong>scripts<\/strong>.<\/p>\n\n\n\n<p>En este cap\u00edtulo hacemos el <strong>m\u00e9todo de la lectura<\/strong>, y en los siguientes, los m\u00e9todos de la <strong>inserci\u00f3n<\/strong>, <strong>actualizaci\u00f3n<\/strong> y <strong>eliminaci\u00f3n<\/strong>.<\/p>\n\n\n\n<p>Sintaxis del archivo <em>crud_axios.html<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&nbsp; &lt;head&gt;\n&nbsp; &nbsp; &lt;meta charset=\"UTF-8\" \/&gt;\n&nbsp; &nbsp; &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/&gt;\n&nbsp; &nbsp; &lt;title&gt;CRUD API REST Axios&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;CRUD API REST Ajax&lt;\/h1&gt;\n&nbsp; &nbsp; &lt;section id=\"crud\"&gt;\n&nbsp; &nbsp; &nbsp; &lt;article&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;h2 class=\"crud-title\"&gt;Agregar nombre&lt;\/h2&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;form class=\"crud-form\"&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type=\"hidden\" name=\"id\" \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type=\"text\" name=\"nombre\" placeholder=\"Nombre\" required \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;br \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type=\"text\"\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name=\"constelacion\"\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder=\"constelaci\u00f3n\"\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;br \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type=\"submit\" value=\"Enviar\" \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/form&gt;\n&nbsp; &nbsp; &nbsp; &lt;\/article&gt;\n&nbsp; &nbsp; &nbsp; &lt;article&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;h2&gt;Ver nombres&lt;\/h2&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;table class=\"crud-table\"&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;thead&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tr&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;th&gt;Nombre&lt;\/th&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;th&gt;Constelaci\u00f3n&lt;\/th&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;th&gt;Acciones&lt;\/th&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/tr&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/thead&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tbody&gt;&lt;\/tbody&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/table&gt;\n&nbsp; &nbsp; &nbsp; &lt;\/article&gt;\n&nbsp; &nbsp; &lt;\/section&gt;\n&nbsp; &nbsp; &lt;template id=\"crud-template\"&gt;\n&nbsp; &nbsp; &nbsp; &lt;tr&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;td class=\"name\"&gt;&lt;\/td&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;td class=\"constellation\"&gt;&lt;\/td&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;button class=\"edit\"&gt;Editar&lt;\/button&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;button class=\"delete\"&gt;Eliminar&lt;\/button&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/td&gt;\n&nbsp; &nbsp; &nbsp; &lt;\/tr&gt;\n&nbsp; &nbsp; &lt;\/template&gt;\n\n&nbsp; &nbsp; &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/axios@1.1.2\/dist\/axios.min.js\"&gt;&lt;\/script&gt;\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; const d = document,\n&nbsp; &nbsp; &nbsp; &nbsp; $table = d.querySelector(\".crud-table\"),\n&nbsp; &nbsp; &nbsp; &nbsp; $form = d.querySelector(\".crud-form\"),\n&nbsp; &nbsp; &nbsp; &nbsp; $title = d.querySelector(\".crud-title\"),\n&nbsp; &nbsp; &nbsp; &nbsp; $template = d.getElementById(\"crud-template\").content,\n&nbsp; &nbsp; &nbsp; &nbsp; $fragment = d.createDocumentFragment();\n&nbsp; &nbsp; &nbsp; const getAll = async () =&gt; {\n\n&nbsp; &nbsp; &nbsp; &nbsp; try {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let res = await axios.get(\"http:\/\/localhost:5555\/santos\"),\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json = await res.data;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json.forEach((el) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $template.querySelector(\".name\").textContent = el.nombre;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $template.querySelector(\".constellation\").textContent =\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el.constelacion;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $template.querySelector(\".edit\").dataset.id = el.id;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $template.querySelector(\".edit\").dataset.name = el.nombre;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $template.querySelector(\".edit\").dataset.constellation =\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el.constelacion;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $template.querySelector(\".delete\").dataset.id = el.id;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let $clone = d.importNode($template, true);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fragment.appendChild($clone);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table.querySelector(\"tbody\").appendChild($fragment);\n&nbsp; &nbsp; &nbsp; &nbsp; } catch (err) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let message = err.statusText || \"Ocurri\u00f3 un error\";\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table.insertAdjacentHTML(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"afterend\",\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `&lt;p&gt;&lt;b&gt;Error ${status}: ${message}\/b&gt;&lt;\/p&gt;`\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; d.addEventListener(\"DOMContentLoaded\", getAll);\n&nbsp; &nbsp; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Para este siguiente ejm tenemos que mandar a llamar a la librer\u00eda Axios, para ello pinchamos en el siguiente enlace:<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1577,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-1598","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"Para este siguiente ejm tenemos que mandar a llamar a la librer\u00eda Axios, para ello pinchamos en el siguiente enlace:","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=1598"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1598\/revisions"}],"predecessor-version":[{"id":1600,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1598\/revisions\/1600"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1577"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}