ajax登錄請(qǐng)求第一次點(diǎn)擊按鈕沒有發(fā)出post請(qǐng)求,但后臺(tái)卻能接收到數(shù)據(jù),且登錄驗(yàn)證流程全都沒能生效,第二次提交的時(shí)候才能正常post,登錄驗(yàn)證流程流程也生效登錄頁面的ajax代碼<script>
???????$("#login_botton").click(function(){
???????????//注冊(cè)按鈕的事件,用ajax提交表單
???????????$.post("/dologin",{
???????????????"username"?:?$("#username").val(),
???????????????"password"?:?$("#password").val()
??????},function(result){
???????????????if(result?==?"1"){
???????????????????//登錄成功
???????????????????alert("登陸成功!將自動(dòng)跳轉(zhuǎn)到首頁");
???????????????????window.location?=?"/";
???????????????}else?if(result?==?"-1"){
???????????????????$("#regist-msg").html("用戶名不存在");
???????????????????$("#regist-msg").addClass("alert-danger")
???????????????}else?if(result?==?"-2"){
???????????????????$("#regist-msg").html("密碼錯(cuò)誤!");
???????????????????$("#regist-msg").addClass("alert-danger")
???????????????}
???????????});
???????});
</script>這是登錄驗(yàn)證的代碼var?formidable?=?require("formidable");var?es6?=?require("es6");var?path?=?require("path");var?fs?=?require("fs");var?db?=?require("./db.js");var?md5?=?require("./md5");
exports.doLogin?=?function?(req,?res,?next)?{????//得到用戶表單
???var?form?=?new?formidable.IncomingForm();
???form.parse(req,?function?(err,?fields,?files)?{????????//得到表單之后做的事情
???????var?username?=?fields.username;????????var?password?=?fields.password;????????console.log(username+password);????????var?jiamihou?=?md5(password);????????//查詢數(shù)據(jù)庫,看看有沒有個(gè)這個(gè)人
???????db.find("users",?{"username":?username},?function?(err,?result)?{????????????if?(err)?{
???????????????res.send("-5");????????????????return;
???????????}????????????//沒有這個(gè)人
???????????if?(result.length?==?0)?{
???????????????res.send("-1");?//用戶名不存在
???????????????return;
???????????}????????????//有的話,進(jìn)一步看看這個(gè)人的密碼是否匹配
???????????if?(jiamihou?==?result[0].password)?{
???????????????req.session.login?=?"1";
???????????????req.session.username?=?username;
???????????????res.send("1");??//登陸成功
???????????????return;
???????????}?else?{
???????????????res.send("-2");??//密碼錯(cuò)誤
???????????????return;
???????????}
???????});
???});
};這是入口文件var?express?=?require('express');var?app?=?express();var?ejs?=?require('ejs');var?route?=?require('./route/route');var?myDo?=?require('./model/do');var?session?=?require('express-session');//使用sessionapp.use(session({
???secret:?'keyboard?cat',
???resave:?false,
???saveUninitialized:?true}));
app.set('view?engine','ejs');//靜態(tài)文件app.use('/static',express.static('public'));
app.use("/avatar",express.static("./avatar"));
app.get('/login',route.showLogin);
app.post('/dologin',myDo.doLogin);當(dāng)我第一次點(diǎn)擊登錄的時(shí)候,從控制臺(tái)看到?jīng)]有post請(qǐng)求,但后臺(tái)卻打印出了賬號(hào)和密碼,同一個(gè)頁面不刷新,我第二次點(diǎn)登錄的時(shí)候,控制臺(tái)就有post請(qǐng)求了,而且賬號(hào)驗(yàn)證的代碼也能正常運(yùn)行
1 回答

村中長相較好的男子
TA貢獻(xiàn)7條經(jīng)驗(yàn) 獲得超4個(gè)贊
找到問題了,是因?yàn)槲业牡卿涰撨€有一個(gè)form 標(biāo)簽,點(diǎn)擊登錄按鈕會(huì)先走那個(gè)form標(biāo)簽的action路徑,所以才提交不到
- 1 回答
- 1 關(guān)注
- 1425 瀏覽
添加回答
舉報(bào)
0/150
提交
取消