sergeda
sergeda

Reputation: 2201

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?

Upvotes: 26

Views: 38443

Answers (2)

Pawan Bishnoi
Pawan Bishnoi

Reputation: 2147

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

Upvotes: 34

Nilesh Mali
Nilesh Mali

Reputation: 560

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

Upvotes: 2

Related Questions