axios
Use axios , to fetch api or for ajax call from react js or react native
install axios by any suitable command based on your environment:
Using npm:
npm install axios
Using bower:
bower install axios
Using yarn:
yarn add axios
Using pnpm:
pnpm add axios
Using jsDelivr CDN:
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
Using unpkg CDN:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
after installation import axios in your component
import axios from "axios"
POST METHOD API CALL
call api by POST Method :
axios.post('mywebsite.com/login.php', {
username: 'mit',
password: '123'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
or same call done by
axios( method: 'post',
url: 'mywebsite.com/login.php',
data: {
username: 'mit',
password: '123'
}
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
call api by GET Method :
axios({
method: 'get',
url: 'userdetail/123',
})
.then(function (response) {
console.log(response);
});
or shorthand for get method
axios('/userdetail/1234');
default method is get method .
so you need to define the method , url and call back function which called after api call completion
so either you can use defualt axios function
axios(config)
config object have url , method and other properties
or other syntax for axios call for all type of request as below:
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
axios support all latest browser.
more detail about axios