我有這項服務:
getCert(p: string): Promise<string> {
return ApiService.getData(this.apiUrl p this.certEndpoint).then(
(response) => response.data
);
}
這是一個例子data:
{
"state": "success",
"message": "Message received."
}
如果我回傳response.data.state或response.data.message獲得相關價值。
如果我用這個來稱呼它:
async componentDidMount() {
const queryString = require('query-string');
const parsed = queryString.parse(location.search);
return this.docListingService.getPks(parsed.policy).then(async pks => {
this.setState({ isLoading: false, packs });
this.certService.getCert(parsed.policy).then( response => {
console.log('RESPONSE: ' response);
});
return pks;
}).catch((error) => {
this.loggingService.logError('Error returning Docs ' error);
this.setState({ errorOccured: true});
});
}
response是一個字串,但我的consle.logprints RESPONSE [object Object]。我試過了console.log('RESPONSE: ' response.state);,console.log('RESPONSE: ' response.message);但他們出錯了。就像這樣:
console.log('RESPONSE: ' JSON.parse(response));
我該如何解決這個問題?
uj5u.com熱心網友回復:
console.log()接受盡可能多的引數。所以你可以像這樣使用它:
console.log('response ', response)
如果要記錄多個變數,可以這樣做:
console.log(variable1, variable2, variable3, 'some string', variable4);
uj5u.com熱心網友回復:
您的代碼的 console.log 中有一個錯誤,您正在嘗試連接字串和 json 物件。你可以試試這段代碼
console.log('response ', JSON.parse(response));
//or
console.log('response ', JSON.parse(response).data.message);
//or
console.log('response ' JSON.parse(response).data.message);
uj5u.com熱心網友回復:
只需使用:
JSON.stringify(response)
這會將物件結構顯示為字串值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/480933.html
標籤:javascript json 休息
上一篇:傳播React道具
