xxxxxxxxxx
import React, { useEffect, useState } from 'react'
import axios from 'axios';
const Dashboard = () => {
const [api, setapi] = useState([]);
const [flag, setflag] = useState(true);
const show = async () => {
const res = await axios.get(`https://crudcrud.com/api/7cd119b8cdd546c5bcd621492ea74cae/article`)
setapi(res.data)
console.log(api);
}
const dele = async (id) => {
await axios.delete(`https://crudcrud.com/api/7cd119b8cdd546c5bcd621492ea74cae/article/${id}`)
setflag(!flag)
}
const additem = async()=>{
const value = prompt("Enter the new value")
const titleValue = prompt("Enter the add Title value")
const article ={"name":value,"title":titleValue}
await axios.post(`https://crudcrud.com/api/7cd119b8cdd546c5bcd621492ea74cae/article`,article)
setflag(!flag)
}
const udate= async(id)=>{
const data = prompt("Enter the data you wanna update")
const title = prompt("Enter the title you wanna update")
await axios.put(`https://crudcrud.com/api/7cd119b8cdd546c5bcd621492ea74cae/article/${id}`,{"name":data,"title":title})
setflag(!flag)
}
useEffect(() => {
show()
}, [flag]);
useEffect(() => {
show()
// additem()
}, []);
return (
<>
<button className='btn btn-primary mr-2' onClick={additem} >Add
</button>
{/* <h1>hello</h1>
<button onClick={getdata}>click me</button> */}
<table className="table">
<thead>
<tr>
<th scope="col">User id</th>
<th scope="col">Name</th>
<th scope="col">Title</th>
</tr>
</thead>
{api.map((val) => {
return (<>
<tbody>
<tr>
<td>{val._id}</td>
<td>{val.name}</td>
<td>{val.title}</td>
<td>
<button className='btn btn-primary' onClick={()=>udate(val._id)} >edit
</button>
<button className='btn btn-danger ml-2' onClick={()=>dele(val._id)}>delete</button>
</td>
</tr>
</tbody>
</>)
})}
</table>
</>
)
}
export default Dashboard
xxxxxxxxxx
import axios from 'axios';
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
xxxxxxxxxx
const axios = require('axios');
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
xxxxxxxxxx
const axios = require('axios').default;
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
// Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
xxxxxxxxxx
import axios from "axios";
axios
.get('https://jsonplaceholder.typicode.com/todos')
.then((res) => {
const data = res.data.json();
})
.catch((err) => {
console.log(err);
});
xxxxxxxxxx
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
// Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
xxxxxxxxxx
const axios = require('axios');
async function makeGetRequest() {
let res = await axios.get('http://webcode.me');
let data = res.data;
console.log(data);
}
makeGetRequest();
xxxxxxxxxx
import axios from 'axios';
axios.get('/user_login', {
params: {
username: 'john1904',
}
})
.then(function (response) {
console.log(response);
})
xxxxxxxxxx
axios.get("URL")
.then(res => {
console.log(res.data);
})
//In axios API also default is GET method
xxxxxxxxxx
import axios from "axios";
/// GET REQUEST
/// Example 1 : Start
// Simple Get Request using Axios
function getData() {
return axios.get("url");
}
/// Example 1 : End
///
/// Example 2 : Start
// Get Request with Params
getData({ page: page, limit: 5, sort: "name", order: "ASC" })
.then((res) => {
// console.log(res);
// op -> {data: Array(5), status: 200, statusText: "OK", headers: Object, config: Object…}
// console.log(res.data);
// op -> [Object, Object, Object, Object, Object]
setData(res.data);
})
.catch((err) => console.log(err));
function getData(params = {}) {
// console.log(params); // op -> {page: 1, limit: 5, sort: "name", order: "ASC"}
return axios.get("url", {
params: {
_page: params.page,
_limit: params.limit,
_sort: params.sort,
_order: params.order
}
});
}
/// Example 2 : End