简体   繁体   English

无法在反应中发出发布请求

[英]Can not make Post request in react

I'm trying to make a POST in react, and I have an error that says {"error_description":"Missing grant type"} in postman works fine, what am I doing wrong? 我正在尝试使POST反应,并且在邮递员中出现一个错误,提示{"error_description":"Missing grant type"}正常,我在做什么错? Thanks! 谢谢!

Here is my code 这是我的代码

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 ) ) 
    }) 

When you make the post request define the type when: URL Encoded but you send a JSON 当您发出发布请求时,请定义以下类型:URL编码但发送JSON

Try to make the request with the next format: 尝试使用以下格式发出请求:

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

Because your values its on the URL Make the post without body 因为您的值在URL上,所以帖子没有正文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM