我想更換一個fetch帶有axios。我的控制臺日志中一直未定義。
async componentDidMount() {
console.log('app mounted');
const tokenString = sessionStorage.getItem("token");
const token = JSON.parse(tokenString);
let headers = new Headers({
"Accept": "application/json",
"Content-Type": "application/json",
'Authorization': 'Bearer ' token.token
});
const response = await axios({
method: 'get',
url: Config.apiUrl `/api/Orders/GetAllInvoices`,
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
'Authorization': 'Bearer ' token.token }
});
console.log(`axios: ${response.json}`)
this.setState({ invoiceList: response.json });
//const response = await fetch(Config.apiUrl `/api/Orders/GetAllInvoices`, {
// method: "GET",
// headers: headers
//});
//const json = await response.json();
//console.log(json);
//this.setState({ invoiceList: json });
...
...注釋掉fetch的作業。我剛剛添加了.json即使axios不需要它。兩種方法都不行。我究竟做錯了什么?
uj5u.com熱心網友回復:
你甚至console.log(response)只是為了看看里面的東西嗎?
我猜你沒有,因為response是一個物件,女巫沒有json鑰匙。你應該使用response.data
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/370579.html
標籤:javascript 反应 公理
