我有一条使用AJAX和expressJS的简单路由,但是我不知道为什么服务器响应永远无法到达客户端。 这是我的index.pug
signupbutton.addEventListener('click',function(e){
var uname = document.getElementById('uname').value;
var psw = document.getElementById('psw').value;
var pswRe = document.getElementById('pswRepeat').value;
var email = document.getElementById('email').value;
// 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.onreadystagechange = function() {
if(this.readyState==4 && this.status==200) {
alert(this.responseText);
}
}
xhttp.open("POST","/SignUpRoute",true);
xhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhttp.send(user);
这是我的服务器端代码
app.post('/SignUpRoute', (req,res) => {
console.log('SignUp Route invoked');
res.status(200).send('Ok');
});
服务器端不会调用客户端的onreadystagechange函数
这是HTML部分
div(id='signupTemplate' class='signupTemplate')
form( class='model-content animate')
div(class='imgcontainer')
span(onclick="document.getElementById('signupTemplate').style.display='none'" class="close" title="Close PopUp") ✖
img(src='/PICS/logo.png')
h2(style='font-size:30px; color:rgb(12, 88, 143); font-weight:bold') Online Compiler Signup
div(class='loginContainer')
p(style='font-size:20px; font-weight:bold; margin-bottom:-7px; color:rgb(0, 162, 255); margin-left:5px;') Username
input(type='text' placeholder='Enter username' name='uname' id='uname')
p(style='font-size:20px; font-weight:bold; margin-bottom:-7px; margin-top:-4px; color:rgb(0, 162, 255); margin-left:5px;') Email address
input(type='text' placeholder='Enter email address' name='email' id='email')
p(style='font-size:20px; font-weight:bold; margin-bottom:-7px; margin-top:-4px; color:rgb(0, 162, 255); margin-left:5px;') Password
input(type='password' placeholder='Enter password' name='psw' id='psw')
p(style='font-size:20px; font-weight:bold; margin-bottom:0; margin-top:-4px; color:rgb(0, 162, 255); margin-left:5px;') Repeat password
input(type='password' placeholder='Enter password' name='psw' id='pswRepeat')
button(type='button' id='templateSignupButton' ) Sign up