2017-01-26 60 views
1

我試圖獲得使用Vue.JS 2和Laravel 5.4的API。 在我App.vue我有以下代碼:Vue.js 2,Laravel 5.4,Passport,axios - API'GET'問題

export default { 
    name: 'app', 
    components: { 
    Hello 
    }, 
    created() { 
    const postData = { 
     grant_type: 'password', 
     client_id: 2, 
     client_secret: 'somesecret', 
     username: '[email protected]', 
     password: 'password', 
     scope: '' 
     } 
     this.$http.post('http://localhost:8000/oauth/token', postData) 
     .then(response => { 
      console.log(response) 
      const header = { 
      'Accept': 'application/json', 
      'Authorization': 'Bearer ' + response.body.access_token 
      } 
      this.$http.get('http://localhost:8000/api/test', {headers: header}) 
      .then(response => { 
       console.log(response) 
      }) 
     }) 
    } 
} 

雖然「POST」工作沒有任何問題,我只是無法得到「GET」工作。

我的console.log顯示以下內容:

Uncaught (in promise) TypeError: Cannot read property 'access_token' 
of undefined at eval (eval at <anonymous> (app.js:810), <anonymous>:33:51) 
(anonymous) @ App.vue?86c0:29 

可惜我找不到爲什麼這可能發生的任何解釋。 是否有人知道他們是否將'access_token'更改爲其他內容或爲何發生這種情況?

希望有人知道:

在此先感謝!

保羅

+0

什麼是您的GET代碼?您無法通過GET發送請求正文。您需要將參數附加到URL。 –

回答

3

這是我的頭頂。 但是我確定如果你使用axios作爲你的XHR客戶端,你的響應體是在'data'參數下,而不是'body'。

總之大家參考ACCESS_TOKEN將

'Authorization': 'Bearer ' + response.data.access_token