{"id":1658,"date":"2024-08-02T10:58:19","date_gmt":"2024-08-02T08:58:19","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1658"},"modified":"2024-08-02T10:58:20","modified_gmt":"2024-08-02T08:58:20","slug":"004-helper-de-peticiones-ajax","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/18-single-page-application-spa\/004-helper-de-peticiones-ajax\/","title":{"rendered":"004. Helper de peticiones AJAX"},"content":{"rendered":"\n<p>En el cap\u00edtulo anterior cre\u00e1bamos las <strong>constantes<\/strong> que vamos a utilizar para conectarnos al sitio del cual vamos a consumir la informaci\u00f3n para nuestro <strong>SPA<\/strong>. El consumo de los datos de una p\u00e1gina hecha en <strong>WordPress<\/strong> lo haremos a trav\u00e9s de <strong>Fetch<\/strong>, para lo cual vamos a crear un <strong>helper<\/strong> que nos ayude a procesar de mejor forma las <strong>peticiones as\u00edncronas<\/strong> con la funci\u00f3n <strong><em>fetch()<\/em><\/strong>. Crearemos por lo tanto un archivo denominado <em>ajax.js<\/em> dentro de la carpeta <em>helpers<\/em>, que tendr\u00e1 la siguiente sintaxis<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-preformatted\">export function ajax(props) {\n&nbsp; let { url, cbSuccess } = props;\n\n&nbsp; fetch(url)\n&nbsp; &nbsp; .then((res) =&gt; (res.ok ? res.json() : Promise.reject(res)))\n&nbsp; &nbsp; .then((json) =&gt; cbSuccess(json))\n&nbsp; &nbsp; .catch((err) =&gt; {\n&nbsp; &nbsp; &nbsp; let message = err.statusText | \"Ocurri\u00f3 un error al acceder a la API\";\n&nbsp; &nbsp; &nbsp; document.getElementById(\"root\").innerHTML = `\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class=\"error\"&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;Error ${err.status}: ${message}&lt;\/p&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/div&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `;\n&nbsp; &nbsp; &nbsp; console.log(err);\n&nbsp; &nbsp; });\n}<\/pre>\n\n\n\n<p>Tambi\u00e9n modificaremos el archivo <em>App.js<\/em>, que contendr\u00e1 la siguiente sintaxis.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import api from \".\/helpers\/wp-api.js\";\nimport { ajax } from \".\/helpers\/ajax.js\";\n\nexport function App() {\n&nbsp; document.getElementById(\n&nbsp; &nbsp; \"root\"\n&nbsp; ).innerHTML = `&lt;h1&gt;Bienvenidos a mi primer SPA con Vanilla Javascript&lt;\/h1&gt;`;\n\n&nbsp; ajax({\n&nbsp; &nbsp; url: api.POST,\n&nbsp; &nbsp; cbSuccess: (posts) =&gt; {\n&nbsp; &nbsp; &nbsp; console.log(posts);\n&nbsp; &nbsp; },\n&nbsp; });\n}\n\nconsole.log(api);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Esquema<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>index.html<\/li>\n\n\n\n<li>app\n<ul class=\"wp-block-list\">\n<li>assets\n<ul class=\"wp-block-list\">\n<li>favicon.png<\/li>\n\n\n\n<li>loader.svg<\/li>\n\n\n\n<li>style.css<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>components<\/li>\n\n\n\n<li>helpers\n<ul class=\"wp-block-list\">\n<li>ajax.js<\/li>\n\n\n\n<li>wp-api.js<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>App.js<\/li>\n\n\n\n<li>index.js<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Archivos<\/h2>\n\n\n\n<p>Estos son los archivos hasta la fecha:<\/p>\n\n\n\n<div class=\"wp-block-file alignleft\"><a id=\"wp-block-file--media-9a87fafb-b111-4d69-a5b4-112df24c183e\" href=\"https:\/\/blog.sutilweb.eu\/wp-content\/uploads\/2024\/08\/spa-1.zip\">spa-1<\/a><a href=\"https:\/\/blog.sutilweb.eu\/wp-content\/uploads\/2024\/08\/spa-1.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-9a87fafb-b111-4d69-a5b4-112df24c183e\">Descarga<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>En el cap\u00edtulo anterior cre\u00e1bamos las constantes que vamos a utilizar para conectarnos al sitio del cual vamos a consumir [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1646,"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-1658","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 el cap\u00edtulo anterior cre\u00e1bamos las constantes que vamos a utilizar para conectarnos al sitio del cual vamos a consumir [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1658","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=1658"}],"version-history":[{"count":3,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1658\/revisions"}],"predecessor-version":[{"id":1662,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1658\/revisions\/1662"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1646"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}