首页 > 解决方案 > 无法在反应中发出 Post 请求

问题描述

我正在尝试在响应中进行 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 ) ) 
    }) 

标签: javascriptreactjsxmlhttprequest

解决方案


当您发出发布请求时定义类型: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 上的值使帖子没有正文


推荐阅读