微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

$.ajax post data error,stay in curren page

 
 
//form
//ajax提交表单 不需要依靠form标签
//我出现的问题是,在有<form action="/login" method="post">....</form>的时候,
//ajax error处理函数,会自动跳转一个页面页面显示返回的结果 例如{msg:'error'}
16 < div class = "login" >
17 < p >
18 < label for = "login" > 用户名 :< /label>
19 < input type = "text" name = "userName" id = "userName" placeholder = '' value = "example" >
20 < /p>
21
22 < p >
23 < label for = "password" > 密码 :< /label>
24 < input type = "password" name = "password" id = "password" placeholder = "example" >
25 < /p>
26
27 < p class = "login-submit" >
28 < button class = "login-button" > Login < /button>
29 < /p>
30 < p class = "forgot-password" > 请输入用户名和密码 < /p>
31 <!-- p class = "forgot-password" >< a href = "index.html" > Forgot your password ?< /a></p-->
32 < /div>
//ajax
34 < script >
35 $ ( document ). ready ( function (){
36 $ ( '.login-button' ). on ( 'click' , function (){
37 var userName = $ ( '#userName' ). val ();
38 var password = $ ( '#password' ). val ();
39 console . log ( userName , password );
40 if ( ! userName . length ){
41 alert ( '请输入用户名!' );
42 $ ( '#userName' ). focus ();
43 return false ;
44 }
45 if ( ! password . length ){
46 alert ( '请输入用户名!' );
47 $ ( '#password' ). focus ();
48 return false ;
49 }
50
51 $ . ajax ({
52 url : '/login' ,
53 type : 'post' ,
54 data : { userName : userName , password : password },
55 dataType : 'json' ,
56 async : true ,
57 success : function ( result ){
58 console . log ( 'success' , result );
59 window . location . href = '/home' ;
62 },
63 error : function ( err ){
64 console . log ( err );
65 $ ( '.forgot-password' )[ 0 ]. innerHTML = '您输入的账号或密码有误,请重新输入' ;
66 }
67 });
68 });
69 });
70 < /script>
//backend
11 exports . login_check = function ( req , res ){
12 var userName = req . body . userName ;
13 var password = req . body . password ;
14 console . log ( '================' , userName , password );
15 if ( userName == 'example' && password == 'example' ){
16 login = true ;
17 req . session . userName = userName ;
18 res . send ( 200 ,{ msg : 'ok' });
19 } else {
20 res . send ( 400 ,68)">'error' });
21 }
22 };

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐