У меня есть форма для проверки Электронная почта и пароль , если она совпадает, она заменит другой код и отобразит Электронная почта и пароль в serverconsole. Поток программы - onclick () для отправки, он переходит на main.js и выполняет newFunction () возвращает POST данные на сервер, т.е. Server .js и замените html-содержимое. Но в моем случае URL-адрес / Login обрабатывается, но адрес электронной почты и пароль не входят в мое условие if в Server.js *
if( emailid == 'abc@g.com' && password == '123' )
{
console.log('Success'+emailid+password);
}
Я перепробовал все обработчики запросов POST, но он не работает.
Home.html
`<!--Login LogOut-->
<div class="container" id ="Loginchange">
<div class="row">
<div class="col" style="padding-center:20px; border-right: 3px solid #84b1aa;"><!--For middle line-->
<!--Existing user-->
<button class="btn btn-primary btn-lg btn-block active" type="button" data-toggle="collapse" data-target="#existingUser" aria-expanded="false" aria-controls="existingUser">
Existing User
</button>
<div class="collapse" id="existingUser">
<div class="card card-body"><!--Start of existing in button-->
<!--Alert with X-->
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Hello Arkodian</strong> Please Log in.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<!--Existing Form-->
<form>
<div class="form-group">
<label for="LoginEmail">Email address</label>
<input type="email" class="form-control" id="LoginEmail" aria-describedby="emailHelp" placeholder="Enter email" data-toggle="tooltip" data-placement="top" title="Email Id" required>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="LoginPassword">Password</label>
<input type="password" class="form-control" id="LoginPassword" placeholder="Password" required>
</div>
<!-- <div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div> -->
<button onclick="newsFunction()" id="Login" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>`
main.js
function newsFunction() {
var Request = new XMLHttpRequest();
Request.onreadystatechange = function(){
if (Request.readyState == XMLHttpRequest.DONE){
if (Request.status == 200){
let loginHtml = Request.responseText;
let ul = document.getElementById('Loginchange');
ul.innerHTML =loginHtml ;
console.log(loginHtml);
}
}
};
Request.open('POST', '/Login', false);
Request.send({
extid: document.getElementById('LoginEmail'),
extpsd:document.getElementById('LoginPassword')
});
}
Server.js
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended :false}));
app.use(bodyParser.json());
app.post('/Login',function(req,res){
var emailid = req.body.extid;
var password = req.body.extpsd;
if( emailid == 'abc@g.com' && password == '123' ){
console.log('Success'+emailid+password);
}
else {
console.log('user not found');
}
var loginHtml = `<p>Hello World</p>`
res.status(200).send(loginHtml);
});