用ajax jquery登录
我使用ajax进行简单登录时出现问题,显然一切都是正确的,但我不知道出了什么问题,因为我需要向客户端发送一条消息,指示用户信息是正确的,然后重定向到主页面。
源代码
<meta http-equiv='Content-type' content='text/html; charset=' /> Red Social Basica Login User/Email Password Enter Sign up $(document).ready(function(){ $('#lnklogin').click(function() { $('#dvlogeo').fadeToggle(200, function() { var divID = $('#dvlistipopubs'); var openDiv = $(this).is(':visible') ? divID : null; }); return false; }); $('#lnkenter').click(function() { var usremail=$('#txtusremail').val(), password=$('#txtpassword').val(); $.ajax({ type: 'POST', url: "account/login.php", data: 'usremail=' + usremail + '&password=' + password, success: function(data) { if(data=='ok'){ document.location="account/main.php" }else{ alert('Access denied'); } } }); return false; }); });
我使用firebug从服务器validation答案,如果用户和密码正确则打印’ok’,如果出现问题则出错,问题是这里不起作用
success: function(data) { if(data=='ok'){ document.location="account/main.php" }else{ alert('Access denied'); } }
我找到的解决方案就是这个,使用json
在我的阿贾克斯
$.ajax({type: 'GET', dataType: 'json', url: "login.php", data: 'usremail=' + usremail + '&clave=' + clave }).done(function(data) { if (data.mensaje == "ok") { document.location = "index.php"; } else { $('#dvmsjlogin').html(data.mensaje); } });
在我的PHP
if ($auth->authenticate()) { $res['mensaje'] = 'ok'; }else{ $res['mensaje']='error'; } echo json_encode($res);
尝试使用此代码与fonction(响应文本);)
success:function(responseText){ // Get the result and asign to each cases if(responseText == ok){ document.location="account/main.php" ; } else{ alert('Access denied'); } } });