问题描述
当我点击提交表单的按钮时出现错误 app.js:
const express = require('express')
const path = require('path')
const app = express();
const bodyParser = require('body-parser')
const Joi = require('joi');
app.use('/public',express.static(path.join(__dirname,'static')));
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.get('/',(req,res) => {
res.sendFile(path.join(__dirname,'static','index.html'))
})
app.post('/',res) => {
console.log(req.body)
const schema = Joi.object().keys({
email : Joi.string().trim().email().required(),password : Joi.string().min(5).max(10).required()
})
Joi.validate(req.body,schema,(err,result) => {
if(err){
res.send('an error has occured')
}
res.send('successfullt posted data')
})
})
app.listen(3000)
/static/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<Meta http-equiv="X-UA-Compatible" content="IE=edge">
<Meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="form">
<form action="/" method="POST" id="form">
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" id="email">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="password">
</div>
<button type="submit">Submit</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script>
$(document).ready(() => {
$('#form').submit((e) => {
e.preventDefault();
$.ajax({
url: '/',type: 'post',contentType: 'application/json',data: JSON.stringify($('#form').serializeArray()),success: (response) => {
console.log('successfully got response')
}
})
})
})
</script>
</body>
</html>
错误:POST http://localhost:3000/ 500(内部服务器错误) 发送@ jquery-3.6.0.js:10109 ajax @ jquery-3.6.0.js:9690 (匿名)@(索引):28 调度@ jquery-3.6.0.js:5430 elemData.handle @ jquery-3.6.0.js:5234
和路径: Here
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)