Fetch API in JavaScript
Fetch API in JavaScript
CLASH J
NEXT -+
( @CODE.CLASH J
Hey Everyone �
�
NEXT+
( @CODE.CLASH J
Fetch API
The Fetch API is a modern interface that
allows you to make HTTP requests to
servers from web browsers.
• If you have worked with
XMLHttpRequest (XHR) object.
• Fetch API can perform all the tasks as
the XHR object does.
___ • Fetch API is much simpler and cleaner.
• It uses the Promise to deliver more
flexible features to make requests to
servers from the web browsers.
NEXT+
3 ( @CODE.CLASH J
Sending a Request
The fetch() method is available in the
global scope that instructs the web
browsers to send a request to a URL.
• The fetch() requires only one
parameter which is the URL of the
resource that you want to fetch.
• When the request completes, the
promise will resolve into a Response
object.
NEXT+
s ( @CODE.CLASH J
fetch{'/readme.txt')
.then{response ⇒ response.text{))
.then{data ⇒ console.log{data));
NEXT+
4 ( @CODE.CLASH J
fetch(url)
.then((response) ⇒ {
})
.catch((error) ⇒ {
}) ;
NEXT+
6 ( @CODE.CLASH J
NEXT+
7 ( @CODE.CLASH J
console.log(response.status);
console.log(response.statusText);
if (response.status 200) {
let data = await response.text();
}
}
fetchText();
NEXT+