{"id":1586,"date":"2024-08-02T07:32:19","date_gmt":"2024-08-02T05:32:19","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1586"},"modified":"2024-08-02T07:32:20","modified_gmt":"2024-08-02T05:32:20","slug":"004-api-rest-crud","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/16-api-rest\/004-api-rest-crud\/","title":{"rendered":"004. API REST: CRUD"},"content":{"rendered":"\n<p>En este cap\u00edtulo lo que vamos a hacer son las 4 operaciones b\u00e1sicas que se le puede hacer a un modelo de datos, es decir, el <strong>CRUD<\/strong>, y lo haremos con las 3 t\u00e9cnicas de <strong>Ajax<\/strong> que vimos en anteriores cap\u00edtulos, el objeto <strong>XMLHttpRequest<\/strong>, el <strong>API Fetch<\/strong>, y la librer\u00eda <strong>Axios<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>En el cap\u00edtulo de hoy vamos a construir la parte <strong>front<\/strong> que necesitar\u00edamos para poder generar esa informaci\u00f3n, y va a tener la siguiente sintaxis:<\/p>\n\n\n\n<p>Sintaxis del archivo <em>crud_ajas.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 Ajax&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\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=\"action\" \/&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 type=\"email\" name=\"email\" placeholder=\"Email\" required \/&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;Email&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=\"email\"&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&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; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo lo que vamos a hacer son las 4 operaciones b\u00e1sicas que se le puede hacer a un [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1577,"menu_order":3,"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-1586","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":"En este cap\u00edtulo lo que vamos a hacer son las 4 operaciones b\u00e1sicas que se le puede hacer a un [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1586","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=1586"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1586\/revisions"}],"predecessor-version":[{"id":1587,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1586\/revisions\/1587"}],"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=1586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}