[英]Pass parameters of a form to a REST API in React
I have a REST API and I want to pass multiple value of a form like parameters using JavaScript and ReactJS when I submit the data. I have a REST API and I want to pass multiple value of a form like parameters using JavaScript and ReactJS when I submit the data.
my problem is send the values of forms in React Hook Forms我的问题是在 React Hook Forms 中发送 forms 的值
This is my API:这是我的 API:
const URL = 'https://localhost:8000'
export const FetchRewards = async (id, serial) => {
const res = await axios.get(`${URL}/api/public/validate_ticket?serial=${serial}&ticket_id=${id}`);
if (res.status !== 200) return [];
return res.data;
};
This my component:这是我的组件:
import { React, useEffect, useState } from 'react'
import { useForm } from "react-hook-form"
import { FetchRewards } from '../services/consultAPI'
export default function RequestModule() {
const [Ticket, setTicket] = useState(null);
const [Res, setRes] = useState(null);
const [TicketDate, setTicketDate] = useState(null);
const [Rewards, setRewards] = useState([]);
const [status, verifyStatus] = useState(null)
const [existRewards, setExistRewards] = useState(false);
const getData = async(id, serial) => {
const data = await FetchRewards(id, serial);
setRes(data);
setTicketDate(data.ticket.fecha_juega);
setRewards(data.premios);
setTicket(data.ticket.ticket);
verifyStatus(data.ticket.status);
setExistRewards(true);
}
useEffect(()=>{
if(!existRewards) getData();
},[existRewards])
const{
register,
handleSubmit,
formState: { errors }
} = useForm();
const onSubmit = (data) => {
console.log(JSON.stringify(data.id))
console.log(JSON.stringify(data.Serial))
};
const petitionRequest = () => {
<div className="consulta">
<img src={bgConsulta} alt="consulta"/>
</div>
}
console.log(onSubmit)
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label>
<p className="mini-text padd">ID</p>
<input {...register("id", { required: true, maxLength: 16 ,pattern: /^\d+$/g })} className="form-control" type="text" placeholder="ID"/>
{errors.id?.type === 'pattern' && <p className="errortext"><i className="fas fa-exclamation-circle"></i> Only accept numbers</p>}
{errors.id?.type === 'required' && <p className="errortext"><i className="fas fa-exclamation-circle"></i> Required</p>}
<div className="spacing"><p className="mini-text">Last 5 digits of Serial</p>
<input {...register("Serial", { required: true, maxLength: 5, minLength: 5, pattern: /^\w+$/g })} className="form-control" type="text" placeholder="Serial"/></div>
{errors.Serial?.type === 'pattern' && <p className="errortext"><i className="fas fa-exclamation-circle"></i> Onlt text and numbers</p>}
{errors.Serial?.type === 'maxLength' && <p className="errortext"><i className="fas fa-exclamation-circle"></i> Only 5 numbers</p>}
{errors.Serial?.type === 'minLength' && <p className="errortext"><i className="fas fa-exclamation-circle"></i> Only 5 numbers</p>}
{errors.Serial?.type === 'required' && <p className="errortext"><i className="fas fa-exclamation-circle"></i> Required</p>}
<div className="spacing">
<button className="btn btn-success submitbtn" type="submit">
SEND
</button>
</div>
</label>
</form>
);
In the arrow function of onSubmit console return the ID in a string and the SERIAL in string too, but my problem is trying to pass those values as parameter in the function FetchRewards(id, serial), I trying to do with onClick in the Submit Button, but it returns: xhr.js:210 GET https://localhost:8000/api/public/validate_ticket?serial=undefined&ticket_id=undefined 500 (Internal Server Error)
when I click the button In the arrow function of onSubmit console return the ID in a string and the SERIAL in string too, but my problem is trying to pass those values as parameter in the function FetchRewards(id, serial), I trying to do with onClick in the Submit按钮,但它返回:
xhr.js:210 GET https://localhost:8000/api/public/validate_ticket?serial=undefined&ticket_id=undefined 500 (Internal Server Error)
当我单击按钮
Try changing the onSubmit of the form尝试更改表单的 onSubmit
Yours你的
<form onSubmit={handleSubmit(onSubmit)}>
To至
<form onSubmit={() => handleSubmit(onSubmit)} />
Or maybe just或者也许只是
<form onSubmit={handleSubmit} />
Seems really strange to have a function executing that way, being that you have to provide a callback function, not code to execute.以这种方式执行 function 似乎真的很奇怪,因为您必须提供回调 function,而不是要执行的代码。
The answer for the problem was:问题的答案是:
const onSubmit = (data) => {
getData(data.id, data.Serial)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.