How to GET HTML from API and Display In DOM using HMPL.js (fetch)?

王林
Release: 2024-07-22 21:14:03
Original
472 people have browsed it

How to GET HTML from API and Display In DOM using HMPL.js (fetch)?

Hello! In this article I would like to talk about how to GET HTML from API and Display In DOM using HMPL.js.

This method is suitable for any api, because This module is based on the Fetch API and almost completely copies the work with the vanilla solution.

Let's say if we take a route that returns HTML in response:

API route - http://localhost:8000/api/test

123
Copy after login

And, let’s say, there is a task in a div with id "wrapper" to display this HTML. To do this, you can connect the hmpl module via the script tag and write the following code:

Copy after login

In this code, thanks to hmpl markup, you can generate a DOM node that can be displayed in HTML. It is worth considering that this node will be updated automatically during the API request process.

If you need to add a request indicator, you can slightly expand the existing code:

Copy after login

In this example, the indicator will be triggered when the request is sent, but the response from the API has not yet arrived.

The above is the detailed content of How to GET HTML from API and Display In DOM using HMPL.js (fetch)?. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!