import { useState } from 'react';
const MyComponent = () => {
const [myValue, setMyValue] = useState('');
const handleSubmit = () => {
fetch(`/api/myEndpoint?value=${encodeURIComponent(myValue)}`)
.then((response) => response.json())
.then((data) => {
// Do something with the data from the server if needed
console.log(data);
})
.catch((error) => {
console.error('Error:', error);
});
};
return (
<div>
<input value={myValue} onChange={(e) => setMyValue(e.target.value)} />
<button onClick={handleSubmit}>Submit</button>
</div>
);
};
字符串 服务器端:
export default function handler(req, res) {
const { value } = req.query;
// use the 'value' received from the client-side
res.status(200).json({ message: `Received value: ${value}` });
}
1条答案
按热度按时间whitzsjs1#
您可以通过请求、查询参数或API端点将客户端值传递给服务器来实现您的目标。
客户端:
字符串
服务器端:
型