Fetch Web API
Luckily, thanks to the fetch
Web API and without any external library, we will not need to use the old fashioned XMLHttpRequest
object.
fetch
usage
fetch
usageThe fetch()
function returns a Promise<Response>
(a promise that resolves to a Response
object).
The Response.json()
method returns a Promise
that resolves the object produced by parsing the JSON content of the body.
Or using async/await
:
fetch
options
fetch
optionsThe second argument for the fetch
function is an options object that allows you for instance to send POST
requests or to control additional stuff in the request like HTTP headers.
In order to avoid security issues like URL forgery, it is recommended to encode the dynamic parts of the URL when constructing it using the encodeURIComponent
function.
Last updated