我在expressjs中有管理员登录表单,然后使用ajax发布变量。在服务器端,我有post功能,但它总是在客户端未定义。请帮助我,如何验证变量。
这是登录表单:
<body>
<div class="container">
<div class="profile">
<button class="profile__avatar" id="btn_avatar">
<img src="/images/avatar.png" alt="Avatar" />
</button>
<div class="profile__form">
<div class="profile__fields">
<h3 class="text-center">Welcome Admin Lelang</h3>
<form>
<div class="fields">
<input type="text" class="input" required-pattern=.*\S.* id="username" placeholder="Username" />
<label class="label" for="fieldUsername">Username</label>
</div>
<div class="fields">
<input type="password" class="input" required-pattern=.*\S.* id="password" placeholder="password" />
<label class="label" for="fieldPassword">Password</label>
</div>
<div class="alert alert-warning" id="warning">
<em class="fa fa-lg fa-warning"> </em>Peringatan, username atau password salah
</div>
<div class="profile__footer">
<center>
<button class="btn" id="btn-submit">LOG IN</button>
</center>
</div>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript" src = "/javascripts/admin/login.js"></script>
</body>
这是ajax:
//===================Fungsi btn submit login===================///
$(function(){
$('#btn-submit').click(function(evt){
evt.preventDefault();
username = $('#username').val();
password = $('#password').val();
console.log(username,password);
$.ajax({
type : 'POST',
data : {username1:username,password1:password},
dataType : 'json',
contentType : 'application/json',
url : '/admin/auth-login',
success : function(data){
console.log("fungsi login berjalan setelah auth-login");
window.location.href = "/admin/";
},
error : function(error){
console.log("fungsi login tidak berjalan setelah auth-login "+error.message);
}
});
});
});
这是服务器端的帖子功能:
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
//Controllers
var posts = require('./routes/posts');
var routes= require('./routes/routes');
var users = require('./routes/users');
var model = require ('../db-mysql');
var admin = express(); // the sub app
admin.use(session(
{
secret: 'AwiLelang',
resave: false,
saveUninitialized: true,
cookie: {
secure: true,
maxAge: 60000
}
}
));
admin.use(bodyParser.json());
admin.use(bodyParser.urlencoded({extended: true}));
// ==============================admin view engine setup==================================================//
admin.set('views', path.join(__dirname, 'views'));
admin.set('view engine', 'ejs');
admin.get('/logout', users);
admin.get('/login', routes);
admin.post('/auth-login', users);
// accept POST request on the admin posts index via posts/
users.post('/auth-login', function (req, res) {
console.log('post login');
if (!req.session.admin) {
var data = {
text: "SELECT * FROM user WHERE ? AND ?",
placeholder: [{username: req.body.username}, {password: /*crypto.MD5(password1).toString()*/ req.body.password}],
}
model.mySQLQuery(data).then(function (result){
console.log(result);
if(result.length){
req.session.admin=req.body.username;
res.send('success');
console.log('success login admin, session admin ke dashboard');
}else{
res.send('failure');
console.log('gagal login admin');
}
}).catch(function(error){
console.log('Error while executing SQL Query', err);
});
}else{
res.redirect('/login');
}
});
这是控制台客户端的屏幕截图: Admin Login Form undefined