javascriptaxioscdn

How to access Axios from plain JS?


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?


Solution

  • Step 1. Import Axios CDN using script tag in tag. For eg.

    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.2.1/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