LoginSignup
138
129

More than 5 years have passed since last update.

axiosライブラリを使ってリクエストする

Last updated at Posted at 2018-10-08

概要

deleteでの通信で割とはまったのでメモっておく。

GET (参照)

クエリパラメータをセットする場合は、URLに直接記述するか、
第2引数にparamsというキー名でセットする。

const queries = { id: '1000' };
Axios
    .get('/api/sample/', {params: queries})
    .then(res => {
        alert("" + queries.id + "」参照完了");
        console.log(res);
    })
    .catch(error => {
        alert("" + queries.id + "」参照失敗");
        console.log(error, queries);
    });

POST (登録)

登録するデータは、第2引数に渡します。

const data = { name: 'hoge' };
Axios
    .post('/api/sample/', data)
    .then(res => {
        alert("" + data.name + "」登録完了");
        this.$router.push({path: '/articles/list'});
    })
    .catch(error => {
        alert("" + data.name + "」登録失敗");
        console.log(error, data);
    });

PUT (更新)

更新するデータは、第2引数に渡します。

const id = '1000';
const modify = { name: 'hoge' };
Axios
    .put('/api/sample/' + id, modify)
    .then(res => {
        alert("" + modify.name + "」更新完了");
        this.$router.push({path: '/articles/list'});
    })
    .catch(error => {
        alert("" + modify.name + "」更新失敗");
        console.log(error, id, modify);
    });

DELETE (削除)

bodyに値をセットする場合は、第2引数にdataというキー名でセットする。

const id = '1000';
const params = { name: 'hoge' };
Axios
    .delete('/api/sample/' + id, {data: params})
    .then(res => {
        alert("" + params.name + "」削除成功");
        this.$router.push({path: '/sample/list'});
    })
    .catch(error => {
        alert("" + params.name + "」削除失敗");
        console.log(error, id, params);
    });

参考サイト

138
129
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
138
129