Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Fetch API in JavaScript
Mursal Furqan Kumbhar
Mursal Furqan Kumbhar

Posted on

     

Fetch API in JavaScript

Hi 👋
Fetching data from a website, or even a local inside your code can be a real headache sometimes. There are many methods that we generally use to get and use data from external sources or files inside our projects. We can even getOur Data from Google Sheetsif we use the correct methods. Here we are going to discuss about theFetch API today.

About Fetch API

The Fetch API is an interface that allows you to make HTTP requests to the server 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 browser.

Sending a Request

Thefetch() method is available in the global scope that instructs the web browsers to send a request to a URL.

  • Thefetch() requires only one parameter, the URL of the resource that you want to fetch.
  • When the request completes, the promise resolves into a Response object.
letresponse_obj_var=fetch(URL);
Enter fullscreen modeExit fullscreen mode

Reading the response

Thefetch() method returns a promise so you can use thethen() andcatch() methods to handle it

fetch(URL).then((response)=>{//handle the responseconsole.log(response)}).catch((error)=>{//handle the errorconsole.warn(error)});
Enter fullscreen modeExit fullscreen mode

Handling Data

If the contents of the response are in the raw format, you use thetext() method.
Thetext() method returns a Promise that resolves with the complete contents of the fetched resource.

fetch('/readme.txt').then(response=>response.text()).then(data=>console.log(data));
Enter fullscreen modeExit fullscreen mode

Besides thetext() method, the response object has different other methods, such asjson(),blob(),formData(),arrayBuffer() and many more, to handle the respective type of data. In practice, you often useasync/await with thefetch() method like this:

asyncfunctionfetchDataFunc(){letresponse=awaitfetch('/readme.txt');letdata=awaitresponse.txt();console.log(data);}
Enter fullscreen modeExit fullscreen mode

Handling the status codes

The response object provides the status code and status text via the status and statusText properties.

asyncfunctionfetchDataFunc(){letresponse=awaitfetch('/readme.txt');console.log(response.status);// returns 200console.log(statusText);// returns OKif(response.status===200){letdata=awaitresponse.text()// handle data}}fetchDataFunc();
Enter fullscreen modeExit fullscreen mode

I hope you would have liked this article. Do comment below or mail me atmursalfurqan@gmail.com to let me know what you want to read next week.

Till then, have an amazing weekend and Happy Coding 🎉.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
artydev profile image
artydev
  • Joined

Thank you

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Trying to make the 🌍 a better place using 🧑‍💻 AWS Community Builder | Love Coding | ☕
  • Location
    Rome, Itlay
  • Education
    M.Sc in Computer Science (Software Engineering)
  • Pronouns
    He/Him/His
  • Work
    Graduate Student at Sapienza University of Rome
  • Joined

More fromMursal Furqan Kumbhar

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp