【问题标题】:Can not make Post request in react无法在反应中发出 Post 请求
【发布时间】:2019-08-02 09:51:14
【问题描述】:

我正在尝试在 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 ) ) 
    }) 

【问题讨论】:

标签: javascript reactjs xmlhttprequest


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2014-01-30
    • 2020-12-24
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多