24
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

axiosからRailsで作ったJSON API(XHR)にアクセスする方法

Last updated at Posted at 2016-07-08

下記コードのようにHTMLとJSON両方に対応しているようなリソースにアクセスする場合は、X-Requested-With をヘッダーに付与しておく必要がある。

respond_to do |format|
  format.html
  format.json { render json: @user }
end

axiosのデフォルトに設定しておくと毎回書かなくて良いので楽。

const axios = require('axios');

axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest';
axios.get('/users/1').then((response) => {
  console.log(response.data);
});
24
18
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
24
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?