[英]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.