{"id":1573,"date":"2024-08-02T07:26:57","date_gmt":"2024-08-02T05:26:57","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1573"},"modified":"2024-08-02T07:26:57","modified_gmt":"2024-08-02T05:26:57","slug":"005-ajax-libreria-axios","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/15-ajax\/005-ajax-libreria-axios\/","title":{"rendered":"005. AJAX: Librer\u00eda Axios"},"content":{"rendered":"\n<p>La librer\u00eda <a href=\"https:\/\/axios-http.com\/es\/docs\/intro\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Axios<\/strong> <\/a>es una <strong>librer\u00eda de terceros<\/strong> basada en promesas que nos puede ayudar mucho a la hora de hacer peticiones <strong>Ajax<\/strong>. Internamente hay algunos <strong>Frameworks<\/strong> que recomiendan usar esta librer\u00eda para hacer peticiones v\u00eda <strong>Ajax<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Axios<\/strong> puede recibir un objeto donde nosotros le especifiquemos la <strong>URL<\/strong>, el <strong>m\u00e9todo<\/strong> por el que estamos haciendo el env\u00edo. La <strong>API<\/strong> de <strong>Axios<\/strong> es muy similar a la <strong>API<\/strong> nativa de <strong>Fetch<\/strong>.<\/p>\n\n\n\n<p>Veamos la sintaxis del ejercicios que vamos a realizar en este cap\u00edtulo para entender el funcionamiento de <strong>Axios<\/strong>, seguiremos tomando como ejm el de los tres cap\u00edtulos anteriores. Veamos la sintaxis de los dos archivos que vamos a generar, llamados <em>axios.html<\/em> y <em>axios.js<\/em>.<\/p>\n\n\n\n<p>Sintaxis de <em>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;Librer\u00eda Axios&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Ajax&lt;\/h1&gt;\n&nbsp; &nbsp; &lt;h2&gt;Librer\u00eda Axios&lt;\/h2&gt;\n&nbsp; &nbsp; &lt;ol id=\"axios\"&gt;&lt;\/ol&gt;\n    &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/axios\/dist\/axios.min.js\"&gt;&lt;\/script&gt;\n&nbsp; &nbsp; &lt;script src=\".\/04axios.js\"&gt;&lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<p>Sintaxis de <em>axios.js<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(() =&gt; {\n&nbsp; const $axios = document.getElementById(\"axios\"),\n&nbsp; &nbsp; $fragment = document.createDocumentFragment();\n\n&nbsp; axios\n&nbsp; &nbsp; .get(\"https:\/\/jsonplaceholder.typicode.com\/users\")\n&nbsp; &nbsp; .then((res) =&gt; {\n&nbsp; &nbsp; &nbsp; let json = res.data;\n&nbsp; &nbsp; &nbsp; json.forEach((el) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; const $li = document.createElement(\"li\");\n&nbsp; &nbsp; &nbsp; &nbsp; $li.innerHTML = `${el.name} --- ${el.email} --- ${el.phone}`;\n&nbsp; &nbsp; &nbsp; &nbsp; $fragment.appendChild($li);\n&nbsp; &nbsp; &nbsp; });\n&nbsp; &nbsp; &nbsp; $axios.appendChild($fragment);\n&nbsp; &nbsp; })\n&nbsp; &nbsp; .catch((err) =&gt; {\n&nbsp; &nbsp; &nbsp; let message = err.response.statusText || \"Ocurri\u00f3 un error\";\n&nbsp; &nbsp; &nbsp; $axios.innerHTML = `Error ${err.response.status} ${message}`;\n&nbsp; &nbsp; })\n&nbsp; &nbsp; .finally(() =&gt; {\n&nbsp; &nbsp; &nbsp; console.log(\n&nbsp; &nbsp; &nbsp; &nbsp; \"Esto se ejecutar\u00e1 independientemente del resultado de Axios\"\n&nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; });\n})();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>La librer\u00eda Axios es una librer\u00eda de terceros basada en promesas que nos puede ayudar mucho a la hora de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1561,"menu_order":4,"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-1573","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":"La librer\u00eda Axios es una librer\u00eda de terceros basada en promesas que nos puede ayudar mucho a la hora de [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1573","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=1573"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1573\/revisions"}],"predecessor-version":[{"id":1574,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1573\/revisions\/1574"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1561"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}