最近编码的时候,突然无缘无故的报错。在寻找解决该问题的方法之后,我输入了一些代码,现在,我得到了一个全新的代码。前端工作在vite上,是用javascript编写的。
import { useState } from 'react'
import './App.css'
import { response } from 'express'
function App() {
const [Password, SetPassword] = useState('')
const [Username, SetUsername] = useState('')
const onSubmit = () => {
fetch('http://localhost:3000', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
Username: Username, // No need to wrap Username in an object
Password: Password
})
})
.then(response => {
if (response.status == 200){
alert('Matched!')
}
})
.catch(error => {
console.error(response)
})
}
return (
<>
<div class='mainDiv --centered'>
<input class='UsernameBox --verticallycentered' placeholder='Username/Email/Phone' value={Username} onChange={(e) => SetUsername(e.target.value)}></input>
<input class='PasswordBox --verticallycentered' placeholder='Password' value={Password} onChange={(e) => SetPassword(e.target.value)}></input>
<h4 className='ErrorThingy'>Passwords can only contain Letters, numbers and symbols</h4>
<button onClick={onSubmit} className='LoginBtn --centered'> Log in</button>
</div>
</>
)
}
export default App
这是因为您正在将express导入到客户端应用程序中。