簡體   English   中英

獲取API POST請求響應

[英]fetch API POST request response

表單提交后,我試圖從發布請求中獲取輸出,但是發布表單時我得到了一個Promise響應而不是實際數據

fetch('localhost/clients', {
  method: 'post',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(formData)
}).then(response => {
  console.info('Sending Message ...')
  console.info(response.json())
}).catch (error => {
  console.log(error)
})

數據被傳遞到后端,但是我想返回API服務器輸出的數據。

response.json()返回一個Promise。 您需要像下面一樣使用它。

fetch('localhost/clients', {
  method: 'post',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(formData)
}).then(response => {
  return response.json();
}).then(jsonResponse => {
  console.log(jsonResponse);
}).catch (error => {
  console.log(error)
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM