javascript - 为什么我无法收到服务器的响应
问题描述
我正在使用 ExpressJS、Ajax 和 MongoDB 构建一个简单的登录/注册系统我目前停留在注册部分,我希望每当用户单击注册按钮时,将电子邮件、帐户和密码发送回服务器,然后服务器将检查该帐户是否已经存在并执行创建用户部分。
if(psw===pswRe)
{
// send uname psw and email to server;
var user = '' + 'uname=' + window.encodeURIComponent(uname) + '&psw=' + window.encodeURIComponent(psw) + '&email=' + window.encodeURIComponent(email);
var xhttp = new XMLHttpRequest();
console.log(user);
xhttp.open('POST','/signup',true);
xhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhttp.send(user);
xhttp.onreadystagechange = function() {
if(this.readyState==4 && this.status==200) {
console.log(this.responseText);
}
if(this.readyState==4&&this.status==100) {
console.log(this.responseText);
}
}
}
这是问题所在,服务器端收到了用户,但它的响应没有调用客户端的“onreadystagechange”函数,所以客户端控制台中基本上没有显示任何内容。这是我的服务器端代码:
app.post('/signup',(req,res)=> {
User.findOne({$or:[{username:req.body.uname},{email:req.body.email}]},function(err,result){
if(err) throw err;
if(result==null)
{
var userData = {
username:req.body.uname,
password:req.body.psw,
email:req.body.email
}
User.create(userData,function(err,user) {
if (err)
throw err;
console.log('account created');
})
res.sendStatus(100);
}
else
{
console.log('username already exists');
res.sendStatus(201);
}
})
});
解决方案
推荐阅读
- java - 如何正确使用 toString 方法?
- javascript - 检查二维数组的元素是否在相等的索引处具有相等的值
- azure-devops - 无法针对 Azure Government Cloud 资源中的 db 运行迁移可执行文件
- ms-access - 由于此表单访问错误,无法删除记录
- ionic3 - Ionic 3 本地通知只触发最后一个
- mysql - MySQLSyntaxErrorException:您的 SQL 语法有错误(Spring Hibernate)
- powershell - 通过 Powershell 设置默认域
- python - python中的“in”关键字是如何工作的?有更快的方法吗?
- c# - 如何将 XAML 文本框绑定到另一个文本框的输入或它自己的输入,然后将输入保存到 MVVM 中的数据对象?
- python-3.x - 无法在任何 IDE 中保存 python 文件