2017-08-10 399 views
0

爲什麼我無法從API獲取JSON響應?我有這個函數向我的API發出axios POST請求。Axios不輸出Console.log,但正在返回JSON數據

handleSubmit(event) { 
    axios.post('/api/v1/user_token/',{ 
     auth: { 
     email: this.state.email, 
     password: this.state.password 
     } 
    }) 
    .then(function(response) { 
    console.log(response.data); 
    console.log(response.statusText); 
    }) 
    .catch(function (error) { 
    console.log(error); 
    }) 
} 

它在Response主體中返回正確的JsonWebToken。但是,當我嘗試設置一個cookie或輸出到console.log,如console.log(response.data)我看不出任何輸出。附有相關圖片。

​​

But no output in the console

+0

有使用'postman'想你的API ... –

+0

好像是端點工作纔剛剛返回用戶令牌,僅此而已。似乎不應該返回任何數據。 – dcodesmith

+0

我會嘗試郵遞員,因爲我看到令牌返回。但是我不知道如何把它設置成Cookie,如果我無法捕捉到它。 – amishpanda

回答

0

我剛剛命名空間錯誤的響應。最終我堅持通過在函數中包含e.preventDefault()來記錄所有內容。

handleSubmit(e) { 
     e.preventDefault(); 
     axios.post('/api/v1/user_token/', 
     {auth: { 
      email: this.state.email, 
      password: this.state.password 
     } 
     }) 
     .then(function(response) { 
     if (response.status !== 201) { 
      console.log('Authentication failed.' + response.status); 
     } 
     return response.data; 
     }) 
    }