【问题标题】:Erorr with post request发帖请求出错
【发布时间】:2021-04-21 20:33:50
【问题描述】:

我正在尝试挑战,我必须通过 发布请求 将这些数据发送到 http://challenge-react.alkemy.org/

email: challenge@alkemy.org
password: react
const baseUrl = "http://challenge-react.alkemy.org/";

const user = {
    'email': 'challenge@alkemy.org',
    'password': 'react'
    
}

const getToken =  async () => {
    fetch(baseUrl, {
        method: 'POST',
        request: {"Content-Type": "application/json"},
        body: JSON.stringify(user)
    })
    .then(response => response.json()) 
    .then(json => console.log(json))
    .catch(err => console.log(err));
}

export default getToken;

由于某种我无法理解的原因,它正在将这个还给我

{错误:“请提供有效的电子邮件和密码”}

我不知道我的问题是我的功能还是什么。 如果我通过邮递员发出同样的请求,它会返回我需要的令牌

【问题讨论】:

    标签: reactjs api post request


    【解决方案1】:

    request 应该是headers

    在此处查看更多信息:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#uploading_json_data

    【讨论】:

      【解决方案2】:

      我认为这在浏览器中永远不会起作用,因为它一直在通过HTTPS 强制请求,错误为:(unknown) POST https://challenge-react.alkemy.org/ net::ERR_SSL_PROTOCOL_ERROR。您可以通过在开发者工具中打开控制台选项卡(Chrome 上的 F12)自行检查。

      function App() {
        const baseUrl = "http://challenge-react.alkemy.org";
      
        const user = {
          email: "challenge@alkemy.org",
          password: "react",
        };
      
        React.useEffect(() => {
          fetch(baseUrl, {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify(user),
          })
            .then((response) => response.json())
            .then((json) => console.log(json))
            .catch((err) => console.error(err)); // "oh, no!"
        }, []);
        return <div />;
      }
      ReactDOM.render(<App />, document.getElementById("root"));
      <div id="root"/>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>

      【讨论】:

        猜你喜欢
        • 2020-06-07
        • 1970-01-01
        • 1970-01-01
        • 2015-03-02
        • 2018-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-15
        相关资源
        最近更新 更多