axios λͺ¨λν
axiosλ ν΅μ μΌλ‘ ν κ³³μμλ§ μ°μ΄λ κ²μ΄ μλκΈ° λλ¬Έμ, μ μ§λ³΄μ λ° κ°λ μ±μ μν΄ λͺ¨λνκ° νμν©λλ€.
μ΄μ κΈμμ μμ±ν νλ‘μ νΈ λλ ν 리 ꡬμ±μ λ°λΌ,
'utils' λλ ν 리μ api.js λΌλ react.js λ₯Ό μμ±ν΄μ μ¬λ¬ νμ΄μ§μμ νμ©νμμ΅λλ€.
api.js μ μΈ
import axios from 'axios';
const API =axios.create({
//baseURL: `http://localhost:8080/common`,
timeout: 30000,
headers: {
"Content-Type" : `application/json;charset=UTF-8`,
"Access-Control-Allow-Origin": "*",
"Accept": "application/json",
}
});
export default API;
νλ‘μ νΈ λ΄ μ¬μ©λ°©λ²
//api μ μΈ
import API from 'utils/api';
//onClick μμ μ¬μ©λ°©λ²
<form action="#" name="loginFrm" method="post" onSubmit={handleSubmit}>
<button type="submit">λ‘κ·ΈμΈ</button>
</form>
const handleSubmit= (e)=>{
e.preventDefault();
async function callAPI(){
const res = await API.post("/login", params)
console.log(res.data)
}
callAPI();
};
//νμ΄μ§ νΈμΆ μ μ¬μ©λ°©λ² useEffect
useEffect(() => {
async function callAPI(){
const res = await API.post("/list", params)
console.log(res.data)
}
callAPI();
}, []);
λ°μν