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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With