[英]Getting POST Request Message using React.JS
我正在編寫一個具有“注冊”功能的程序。 前端是使用React.JS創建的。 到目前為止,我已經能夠使用此代碼在React.JS中發送發布請求:
fetch('http://localhost:2000/users/signup/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"email": "testing@gmail.com",
"password": "secret",
"name": "matthew",
"organization": "Apple"
})
}).then(function(response) {
return response.json();
});
這非常有效-因為用戶信息現在在數據庫中。 但是我不確定如何使用response.json()獲取響應JSON。 我希望能夠獲得響應,獲取消息字符串,並將其顯示在前端的我的用戶上。 這是我在Postman上運行相同的發布請求時的響應。
{
"message": "New user created"
}
謝謝您的幫助!
response.json()
返回一個承諾,所以你需要一個更then
獲得實際的數據:
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
})
參見https://developer.mozilla.org/en-US/docs/Web/API/Body/json
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.