Created
August 10, 2017 09:17
-
-
Save graffiti-withwind/cd4de52562eefbd6101afecdcbf7f7bf to your computer and use it in GitHub Desktop.
[Laravel5.4] APIトークン認証
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
/* | |
* Laravel 5.4 の routes/api.php に書かれているルーティングにアクセスする。 | |
* | |
* Laravel-mixで導入される通信ライブラリ「axios」を使ったサンプル。 | |
* [axios] https://github.com/mzabriskie/axios | |
* | |
* apiにアクセスする場合はユーザー(usersテーブル)のapi_tokenの値が必要になる。 | |
*/ | |
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; | |
token = document.head.querySelector('meta[name="csrf-token"]'); | |
if (token) { | |
axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content; | |
} else { | |
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token'); | |
} | |
axios.get('{{url('api/user')}}', { | |
params : { | |
'api_token': '[users.api_tokenの値]l' | |
} | |
}) | |
.then(function(responce){ | |
console.log(responce); | |
}) | |
.catch(function(error){ | |
console.log(error); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment