React | Axios
Axiosとは?
📌 axios vs fetch
共通点
HTTPプロトコルによるサーバとの通信
ひかくひょう
📌 特長
変換
📌 使用方法
◼ HTTP Methods
GET
urlに存在するリソースの入力を要求
axios.get(url,[,config])
// 예시 1
import axios from 'axios';
axios.get('https://my-json-server.typicode.com/zofqofhtltm8015/fs/user').then((Response)=>{
console.log(Response.data);
}).catch((Error)=>{
console.log(Error);
})
// 예시 1 결과
[
{ id: 1, pw: '1234', name: 'JUST' },
{ id: 2, pw: '1234', name: 'DO' },
{ id: 3, pw: '1234', name: 'IT' }
]
// 예시 2
// GET request for remote image
axios({
method: 'get',
url: 'http://bit.ly/2mTM3nY',
responseType: 'stream'
})
.then(function (response) {
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
});
// 예시 3
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
// always executed
});
POST
新規リソースの作成(create)
axios.post("url주소",{
data객체
},[,config])
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
axios.post("url", {
username: "",
password: ""
})
.then(function (response) {
// response
}).catch(function (error) {
// 오류발생시 실행
}).then(function() {
// 항상 실행
});
DELETE
RESTベースのAPIプログラムでデータベースに格納されているコンテンツを削除する
axios.delete(URL,[,config]);
axios.delete("/thisisExample/list/30").then(function(response){
console.log(response);
}).catch(function(ex){
throw new Error(ex)
}
PUT
RESTベースのAPIプログラムでデータベースに格納されているコンテンツを更新する
axios.put(url[, data[, config]])
axios.put("url", {
username: "",
password: ""
})
.then(function (response) {
// response
}).catch(function (error) {
// 오류발생시 실행
}).then(function() {
// 항상 실행
});
Reference
この問題について(React | Axios), 我々は、より多くの情報をここで見つけました https://velog.io/@katej927/Axiosテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol