Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
95
Help us understand the problem. What is going on with this article?
@reflet

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

More than 1 year has passed since last update.

概要

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);
    });

参考サイト

95
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
reflet

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
95
Help us understand the problem. What is going on with this article?