To call an API in JavaScript, you can use several methods, including fetch()
, XMLHttpRequest
, and third-party libraries like Axios.
Here's an example of calling an API using fetch()
:
index.tsx154 chars5 lines
This sends a GET request to the specified API endpoint, retrieves the response as JSON, and logs it to the console. If an error occurs, it will also be logged to the console.
Here's an example of calling an API using XMLHttpRequest
:
index.tsx252 chars12 lines
This sends a GET request to the specified API endpoint, retrieves the response as JSON, and logs it to the console. If an error occurs, it will also be logged to the console.
And finally, here's an example of calling an API using the Axios library:
index.tsx134 chars4 lines
This uses the Axios library to send a GET request to the specified API endpoint, retrieves the response as JSON, and logs it to the console. If an error occurs, it will also be logged to the console.
gistlibby LogSnag