0 ratings0% found this document useful (0 votes) 27 views9 pagesFetch API in JavaScript ??
Fetch api in js with tricks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
©
Fetch API In
JavaScript°
Hey Everyone ©
Javascript is everywhere. Millions of
webpages are built on JS.
in this Post, you'll learn about the
JavaScript Fetch API and how to use
it to make asynchronous HTTP
requests.
Do Like, save and Share This Post If
You Found This Helpful.e
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.e
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.
et response =e
The text() method returns a Promise that
resolves with the complete contents of
the fetched resource.
response. QO)
console. (data) );
+ Besides the text() method, the
Response object has other methods
such as json(), blob(), formData() and
arrayBuffer() to handle the respective
type of data.oO
Reading the Response
The fetch() method returns a Promise so
you can use the then() and catch()
methods to handle it.
+ If the contents of the response are in
the raw. text format, you can use the
text() method.oe
In practice, you often use the async/
await with the fetch() method like this:
async Func rom on
response =
Ce ee aS Le
console. (data);( @CODE.CLASH )
The object provides the
and via the and
properties.
asyne function lomne
let response = await ('/readme.txt');
console. (response.status) ;
console. (response. statusText) ;
if (response.status 200) {
let data = await response. OFDid you find it
Useful?
Leave a comment!
Alamin CodePapa
H (Maree Lele :
3 » 5
a (eee Repost
© ) tc)