DEV Community

Cover image for How to make AJAX requests using AXIOS?
Naveen.S
Naveen.S

Posted on

How to make AJAX requests using AXIOS?

The first step is to install the AXIOS library on the project that will need to work with AJAX. And there are some ways to do this, one of them is to use a dependency manager to download, another and simply include the library's online CDN in the project.

Download AXIOS with npm:

npm install axios

Download AXIOS with yarn:

yarn install axios

Using CDN:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

The purpose of the AXIOS library is simply to make AJAX requests, it means that it is a very small and light library. In addition to having a very simple and lean implementation.

Another important feature is the compatibility with several different browsers.

AXIOS Syntax

Basically it uses the AXIOS resource, or HTTP verb of the request (get, post, put, patch, delete, options, head), and uses the then() method to retrieve the promise, which is returned in case of success, and the method catch() which is the return of an exception.

The get() method expects two parameters, the first is the URL that will be made the AJAX request, and the second is optional, which is the data (for meters) that will be sent.

axios.get ('https://viacep.com.br/ws/${cep}/json/')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.log(error);
  })

But, what is AXIOS?

Axios is a promise-based HTTP client for the browser and Node. js. Axios makes it easy to send asynchronous HTTP requests to REST endpoints and perform CRUD operations. It can be used in plain JavaScript or with a library such as Vue or React.

Oldest comments (0)