當我發出這樣的發布請求時,為什么我的令牌未定義:
let requestInfo = JSON.stringify({
email: email,
password: password,
});
let requestOptions = {
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: requestInfo
}
fetch('http://url/token', requestOptions).then(response => {
response.json()
setShowLoading(false);
}).then(token => console.log(token)).catch(error => console.log(error))
令牌 => console.log(token) 始終未定義,當我使用錯誤的憑據登錄時 .catch(error => console.log(error)) 不會觸發
但是當我發出這樣的帖子請求時,效果很好:
const response = await fetch('http://url/token', requestOptions);
const data = await response.json();
console.log(data);
uj5u.com熱心網友回復:
您需要response.json()在then子句中回傳,以便在下一個then子句中使用它。
fetch('http://url/token', requestOptions).then(response => {
setShowLoading(false);
return response.json()
}).then(token => console.log(token)).catch(error => console.log(error))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/515229.html
上一篇:LSTM應該放在我的NLPCNN中的什么位置,如何連接它?
下一篇:REST相關/嵌套物件URL標準
