簡體   English   中英

是否有任何正確的方法可以使用 fetch() 通過 React 發送 POST 請求?

[英]Is there any proper way to send a POST request through React using fetch()?

I am getting url as None on the API server side and the API is working fine when tested with POSTMAN and other scripts but the problem happens when sending the request in React .

const imgLoad = {
  method: 'POST',
  headers: {
    "Content-Type":"application/json"
  },
  body: JSON.stringify({
    image_url: imageurl    //image url stored in a variable & I tried using a string but the problem is still there
  })
}

fetch("http://127.0.0.1:7000/emotion_detection/detect/", imgLoad)
  .then(response => response.json())
  .then(data => this.setState({emotion: data}));

使用POSTMAN工具從 ReactJS 中找到正確的實現方式。

var formdata = new FormData();
formdata.append("image_url", `${this.state.input}`);

var requestOptions = {
  method: 'POST',
  body: formdata,
  redirect: 'follow'
};

fetch("http://127.0.0.1:7000/emotion_detection/detect/", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

暫無
暫無

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

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