【问题标题】:Axios post request failed with status code 400Axios 发布请求失败,状态码为 400
【发布时间】:2021-10-18 17:11:01
【问题描述】:

我收到以下请求的状态码 400,但它在邮递员中工作正常。

function App() {
  function sendRequest(){
    let body={
        client_id: 'val1',
        client_secret: 'val2',
        code: '12345',
        grant_type: 'authorization_code',
        redirect_uri: 'exp://127.0.0.1:19000'
    };
    let config = {
      headers:{
        'Content-Type': 'application/x-www-form-urlencoded',
      }
    }
    axios.post('https://www.example.com',body,config).then((res)=>{
      console.log("The response is: "+res)
    }).catch(err=>console.log("The error is: "+err))
  }

  return (
    <div className="App">
     <button onClick={()=>sendRequest()} >Run</button>
    </div>
  );
}

这是我得到的控制台输出,The error is: Error: Request failed with status code 400

【问题讨论】:

  • ` 'Content-Type': 'application/json' ` 请尝试使用 contet-type application json。
  • 另一个常见的错误可能是:在邮递员上,您正在执行postget
  • 你确定要发送给example.com吗?如果您拥有服务器代码,您也应该在此处发布它。否则,请告诉我们您的目标是什么?

标签: javascript reactjs axios


【解决方案1】:

试试这个方法:

function App() {
      function sendRequest(){
        let params =new URLSearchParams();
        params.append('client_id','val1');
        params.append('client_secret','val2');
        params.append('code','12345');
        params.append('grant_type','authorization_code');
        params.append('redirect_uri','exp://127.0.0.1:19000');
        axios.post('https://www.example.com',params).then((res)=>{
          console.log("The response is: "+res)
        }).catch(err=>console.log("The error is: "+err))
      }
    
      return (
        <div className="App">
         <button onClick={()=>sendRequest()} >Run</button>
        </div>
      );
    }

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 2021-04-07
    • 2021-05-18
    • 1970-01-01
    • 2021-01-12
    • 2017-06-02
    • 2019-09-29
    • 2018-02-25
    • 1970-01-01
    相关资源
    最近更新 更多