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