Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Axios from plain JS? [closed]

Tags:

javascript

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?

like image 241
sergeda Avatar asked Aug 10 '20 04:08

sergeda


People also ask

Can we use Axios in vanilla JavaScript?

Axios is similar to the fetch API and it can be used in plain JavaScript as well as in modern JavaScript frameworks like React, Angular, and Vue.

Can you use Axios in HTML?

Axios is a strictly Promise-based library, and automatically converts the data that it fetches from the API from JSON format. To use Axios in your project, you need to either install it from the npm library or add it in your HTML file by using a CDN hosted by Axios.

Can we use Axios in backend?

Sending HTTP requests to your API with Axios is a fantastic tool. Axios is supported by all major browsers. The package can be used for your backend server, loaded via a CDN, or required in your frontend application.


1 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

like image 131
Pawan Bishnoi Avatar answered Sep 29 '22 23:09

Pawan Bishnoi