简体   繁体   English

为什么我在运行 React 应用程序并单击表单提交时出现此错误

[英]why i am getting this error while i run react app and click submit in form

App.js应用程序.js

import React from "react";
import { useState } from "react";
import axios from 'axios'
const App=()=>{
  const [data,Setdata]=useState({name:'',password:''});
  
  const Handler=e=>{Setdata({...data,[e.target.name]:e.target.value})}
  const submitHandler=e=>{
    e.preventDefault();
    axios.post('https://charming-league-352616-default-rtdb.firebaseio.com/register.json',data).then(()=>alert("submittted successfully"))
  


  }
  return(
    <div>
      <center>
      <form onSubmit={submitHandler}>
      <label style={{'color':'red'}}>Name:</label><br/>
      <input type="text" name="name"onChange={Handler}/><br/>
      <label style={{'color':'pink'}}>Passoword</label><br/>
      <input type="password" password="age" onChange={Handler}></input><br/>
      <input type="submit" value="submit"></input>
</form>
</center>
    </div>
  )
}
export default App;

Error错误

POST https://charming-league-352616-default-rtdb.firebaseio.com/register.json 400 (Bad Request)

Uncaught (in promise) AxiosError {message: 'Request failed with status code 400', name: ' 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}未捕获(承诺)AxiosError {消息:'请求失败,状态代码 400',名称:''AxiosError',代码:'ERR_BAD_REQUEST',配置:{...},请求:XMLHttpRequest,...}

The REST API of the Firebase Realtime Database expects the body of the request to contain a JSON object, and it seems like your code is not passing that. Firebase实时数据库的 REST API 期望请求正文包含 JSON object,看起来您的代码没有通过它。

I recommend console.log(JSON.stringify(data)) right before the Axios call to see what the data is that you're passing.我建议在 Axios 调用之前使用console.log(JSON.stringify(data))来查看您传递的数据是什么。

See the documentation on writing data with a POST request .请参阅有关使用 POST 请求写入数据的文档。

Seems Submit works fine and the problem Detects in your api request似乎提交工作正常,问题在您的 api 请求中检测到

Try check your request in PostMan尝试在 PostMan 中查看您的请求

暂无
暂无

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

相关问题 firebase.database 不是 function 为什么我在反应本机应用程序中收到此错误? - firebase.database is not a function why I am getting this error in react native app? 为什么我在 iOS(颤振应用程序)中收到 Firebase AppCheck 错误 - Why am I getting a Firebase AppCheck error in iOS (Flutter app) 为什么在运行以下 SQL 代码时出现错误 - Why am I getting an error while running the following SQL code 从firestore(firebase)获取数据时出现错误。 我正在使用本机反应 - I am getting an error while fetching data from firestore(firebase). I am using react native 我在制作新的反应时遇到错误 - 本机应用程序 - I am getting an error when i was making a new react - native app 我收到 First argument "email" must be a valid string error while firebase authentication in react - I am getting First argument "email" must be a valid string error while firebase authentication in react 为什么我没有从反应 redux state 获得更新的值 - Why I am not getting the updated value from react redux state 将日期数据上传到 flutter 中的 firestore 时出现错误 - I am getting error while uploading date data to firestore in flutter 在这种情况下,为什么我会收到 Bad state 错误? - Why am I getting a Bad state error in this case? 为什么我会收到“从上游读取响应 header 时暂时禁用上游服务器”? - Why am I getting "upstream server temporarily disabled while reading response header from upstream"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM