我正在嘗試使用表單在完整的堆疊 MERN 應用程式中將資訊從客戶端發送到服務器端,但表單未提交這里是代碼:
import React, { useState } from 'react'
import axios from 'axios';
export default () => {
//keep track of what is being typed via useState hook
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
//handler when the form is submitted
const onSubmitHandler = e => {
//prevent default behavior of the submit
e.preventDefault();
//make a post request to create a new person
axios.post('http://localhost:8000/api/people', {
firstName,
lastName
})
.then(res=>console.log(res))
.catch(err=>console.log(err))
}
//onChange to update firstName and lastName
return (
<form onSubmit={onSubmitHandler}>
<p>
<label>First Name</label><br/>
<input type="text" onChange={(e)=>setFirstName(e.target.value)} value={firstName}/>
</p>
<p>
<label>Last Name</label><br/>
<input type="text" onChange={(e)=>setLastName(e.target.value)} value={lastName}/>
</p>
<input type="submit"/>
</form>
)
}
我收到以下錯誤:

我無法找出問題所在,仔細檢查了所有在互聯網上搜索的內容 = 沒有
請幫忙!
uj5u.com熱心網友回復:
問題似乎是您的 NodeJS 服務器沒有在后臺運行。您應該在通過后端發出任何 API 請求時運行它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/464304.html
