LoginSignup
7
6

More than 3 years have passed since last update.

脱jQueryを目指してajaxメソッドをaxiosに置き換えてみる

Last updated at Posted at 2019-10-25

脱jQueryを目指してajaxメソッドをaxiosに置き換えます。

準備

axiosを使うために以下のサイトからjsファイルをダウンロードして読み込んでおきます。

<script src="axios.min.js"></script>

GET通信

// jQuery
$.ajax({
  type: 'GET',
  url: 'http://localhost/test',
  params: { name: 'Taroimo' },
}).then(...);

// axios (1)
axios.get(
  'http://localhost/test',
  { name: 'Taroimo' }
).then(...);

// axios (2)
axios({
  method: 'GET',
  url: 'http://localhost/test',
  params: { name: 'Taroimo' }
}).then(...);

POST通信

// jQuery
$.ajax({
  type: 'POST',
  url: 'http://localhost/test',
  data: { name: 'Taroimo' },
}).then(...);
// axios (1)
axios.post(
  'http://localhost/test',
  { name: 'Taroimo' }
).then(...);
// axios (2)
axios({
  method: 'POST',
  url: 'http://localhost/test',
  data: { name: 'Taroimo' }
}).then(...);
7
6
1

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
7
6