How to access Axios from plain JS?

Viewed 11531

I'm trying to use Axios in plain JS. I've included it from CDN. In the docs they are creating Axios object like this: const axios = require('axios');

How can I do the same without Node with plain JS?

2 Answers

Step 1. Import axios CDN using script tag in tag. For eg.

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>

Step 2. Use axios in script tag like this :-

<script>
function handleRequest() {
    axios.post("url", {name: "data"}).then(function (response) {
        console.log(response)
        // do whatever you want if console is [object object] then stringify the response
    })
}

You can surely use axios in html file without nodeJS.Happy Coding

In browser environment you don't need to import/require axios, it is available to use globally with axios variable.

Related