任务是从html开始,通过客户端打字稿和服务器打字稿为我的sql数据库编写一条用于用户注册的创建路由。我什至不知道该错误是否出在我的客户端或服务器上。
我已经尝试使用控制台和调试器对其进行调试。运行服务器可以正常工作,而加载.js也可以。因此,我认为错误必定是在client.ts上的功能或者在server.ts上的功能/路由(或者可能是两者)上。
client.ts
function registrierung(event: Event): void {
event.preventDefault();
const vnamex: string = vname.val().toString();
const nnamex: string = nname.val().toString();
const strassex: string = strasse.val().toString();
const hnrx: string = hnr.val().toString();
const plzx: string = plz.val().toString();
const ortx: string = ort.val().toString();
const emailx: string = email.val().toString();
const passwortx: string = passwort.val().toString();
$.ajax({
contentType: "application/x-www-form-urlencoded",
data: "vname=" + vnamex + "&nname=" + nnamex +
"&strasse=" + strassex + "&hnr=" + hnrx + "&plz=" + plzx + "&ort=" + ortx +
"&email=" + emailx + "&passwort=" + passwortx,
type: "POST",
url: "/register",
});
}
server.ts
const createQuery: string = "INSERT INTO person" + "(vname, nname, strasse, hnr, plz, ort, email, passwort) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
router.post("/register", regserver);
function regserver(req: express.Request, res: express.Response) {
const vname: string = req.body.vname;
const nname: string = req.body.nname;
const strasse: string = req.body.strasse;
const hnr: string = req.body.hnr;
const plz: string = req.body.plz;
const ort: string = req.body.ort;
const email: string = req.body.email;
const passwort: string = req.body.passwort;
connection.query(createQuery, [vname, nname, strasse, hnr, plz, ort, email, passwort],
(err: mysql.MysqlError | null) => {
if (err === null) {
res.sendStatus(201);
} else {
res.sendStatus(500);
}
});
}
因此,来自html输入字段的数据应在sql表“ person”中创建新行。