Javascript 为什么我的网站需要重新加载才能正常工作
我的express.js服务器有问题。 我用端口3000在本地托管我的网页 在我的网站上,我有一个用于注册/登录用户的功能。启动服务器后,URL如下所示:http://localhost:3000/ 如果我的URL看起来像这样,并且我尝试注册/登录,我的网站只会重新加载 注册/登录失败并重新加载后,URL将更改为以下内容:http://localhost:3000/?# 当URL看起来像这样时,注册/登录工作非常正常 我的注册函数如下所示:Javascript 为什么我的网站需要重新加载才能正常工作,javascript,html,express,Javascript,Html,Express,我的express.js服务器有问题。 我用端口3000在本地托管我的网页 在我的网站上,我有一个用于注册/登录用户的功能。启动服务器后,URL如下所示:http://localhost:3000/ 如果我的URL看起来像这样,并且我尝试注册/登录,我的网站只会重新加载 注册/登录失败并重新加载后,URL将更改为以下内容:http://localhost:3000/?# 当URL看起来像这样时,注册/登录工作非常正常 我的注册函数如下所示: async function onRegisterSu
async function onRegisterSubmit() {
obj = {
username: document.getElementById("username").value,
email: document.getElementById("email").value,
password: document.getElementById("password").value
}
if (validation(obj)) {
return;
}
const body = JSON.stringify(obj);
const res = await fetch("/register", {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: body
});
if (res.status === 201) {
alert("Registered succesfully.");
closeForm();
} else {
alert("There was an error.");
}}
app.post('/register', function (req, res) {
if ( !req.body.email
|| !req.body.password
|| !req.body.username
|| typeof req.body.email !== 'string'
|| typeof req.body.password !== 'string'
|| typeof req.body.username !== 'string'
) {
res.sendStatus(400);
}
users.push({ email: req.body.email, passwordDerivative: encrypt(req.body.password), username: req.body.username });
lists.push({ username: req.body.username, lists: []})
res.status(201).send('Registered user with mail: ' + req.body.email);})
我的express.js/register如下所示:
async function onRegisterSubmit() {
obj = {
username: document.getElementById("username").value,
email: document.getElementById("email").value,
password: document.getElementById("password").value
}
if (validation(obj)) {
return;
}
const body = JSON.stringify(obj);
const res = await fetch("/register", {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: body
});
if (res.status === 201) {
alert("Registered succesfully.");
closeForm();
} else {
alert("There was an error.");
}}
app.post('/register', function (req, res) {
if ( !req.body.email
|| !req.body.password
|| !req.body.username
|| typeof req.body.email !== 'string'
|| typeof req.body.password !== 'string'
|| typeof req.body.username !== 'string'
) {
res.sendStatus(400);
}
users.push({ email: req.body.email, passwordDerivative: encrypt(req.body.password), username: req.body.username });
lists.push({ username: req.body.username, lists: []})
res.status(201).send('Registered user with mail: ' + req.body.email);})
这里有什么问题?我怎样才能让我的网站正常运行而不必先重新加载它
提前感谢。请在我提供了一些代码的情况下发表评论,如果有帮助,请告诉我。如果你需要别的东西,我可以提供。