Laravelで作ったAPIをVue.jsで作ったSPAからAxiosを使って呼び出しています。
同じサーバーの他のAPIにはGET,POSTできるのに、oauth/tokenのみアクセスできません。
サーバー側ではcorsのセットアップをし、クロスドメインに対応しました。
同じサーバー上の他のAPIに対しては認証して操作等できています。
思い当たることがあれば教えてください。
./repositories/Repository.js
js
1import axios from 'axios' 2 3const baseDomain = '***.****.io' 4const baseURL = `https://${baseDomain}` 5 6export default axios.create({ 7 baseURL: baseURL, 8 headers: { 9 'Content-Type': 'application/json', 10 'Accept': 'application/json', 11 'Access-Control-Allow-Origin': '*', 12 'X-Requested-With': 'XMLHttpRequest' 13 }, 14 responseEncoding: 'utf8', 15 responseType: 'json' 16}) 17
./repositories/authRepository.js
js
1import Repository from './Repository' 2 3export default { 4 // ******動かず*********************************** 5 loginUser () { 6 var data = { 7 'client_id': 2, 8 'client_secret': '******************', 9 'username': 'test@test.com', 10 'password': 'testtest', 11 'grant_type': 'password', 12 'scope': '' 13 } 14 return Repository.post('oauth/token', data) 15 }, 16 17 // ******動く*********************************** 18 getUser () { 19 Repository.defaults.headers.common['Authorization'] = `Bearer *************` 20 return Repository.get('api/user', {}) 21 }, 22 23 // ******動く*********************************** 24 cerateUser () { 25 var data = { 26 'client_id': 2, 27 'client_secret': '********************', 28 'name': 'username', 29 'email': 'test@example.com', 30 'password': 'abc123', 31 'grant_type': 'password', 32 'scope': '' 33 } 34 return Repository.post('api/users', data) 35 } 36}
./components/PageLogin.vue
js
1import { RepositoryFactory } from './../repositories/RepositoryFactory' 2const AuthRepository = RepositoryFactory.get('auth') 3 4export default { 5 name: 'login', 6 data () { 7 return { 8 isLoading: false, 9 posts: [] 10 } 11 }, 12 created: function () { 13 this.fetch() 14 }, 15 methods: { 16 async fetch () { 17 this.isLoading = true 18 const { data } = await AuthRepository.loginUser() 19 this.isLoading = false 20 this.posts = data 21 console.log(data) 22 } 23 } 24}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。