簡體   English   中英

無法在反應中發出發布請求

[英]Can not make Post request in react

我正在嘗試使POST反應,並且在郵遞員中出現一個錯誤,提示{"error_description":"Missing grant type"}正常,我在做什么錯? 謝謝!

這是我的代碼

class App extends Component {
  constructor() {
    super()
    this.state = {
      info : null
    }
  }

  componentDidMount() {
    var payload = {
      client_id: 'my_site',
      grant_type: 'my_credentials',
      client_secret: 'xxx',   
    }
    var data = new FormData();
    data.append("json",JSON.stringify(payload));

     fetch('/myendpoint', {
        method: "POST",
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        },
        body: data
        })
   })
    .then(function(res){ 
      return res.json(); 
    })
    .then(function(data){ 
      alert( JSON.stringify( data ) ) 
    }) 

當您發出發布請求時,請定義以下類型:URL編碼但發送JSON

嘗試使用以下格式發出請求:

 fetch('/myendpoint?client_id="some id"&grant_type="some type"', { method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } }) 

因為您的值在URL上,所以帖子沒有正文

暫無
暫無

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

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