简体   繁体   English

Javascript AJAX登录不正常

[英]Javascript AJAX Login not behaving

I am creating an ajax login and it used to work but for some reason now it redirects me to my php login processing page. 我正在创建一个ajax登录,它曾经可以工作,但是由于某种原因,它现在将我重定向到我的php登录处理页面。 The code is below and I am using jquery ajax to submit my ajax request. 代码在下面,我正在使用jquery ajax提交我的ajax请求。 The weird thing is that it apparently isn't sending the variables because my page doesn't echo the word 'success'. 奇怪的是,它显然没有发送变量,因为我的页面没有回显“成功”一词。 Any help would be appreciated and also, would this be secure to use? 任何帮助将不胜感激,并且,这将是安全使用吗?


Javascript(jQuery); Javascript(jQuery);

$('.mainlogin').submit(function() {
    var username = $('#main_username').val();
    var password = $('#main_pword').val();
    $.ajax({
        url: 'log.php',
        type: 'POST',
        data: {
            user: username,
            pass: password
            },
        success: function(response) {
            if (response == 'success') {
                    window.location.reload();
            } else {
                $('.logerror').fadeIn(250);
            }
        }
    });
    return false;
});

PHP; PHP;

<?php 
require_once('.conf.php');
$user = mysql_real_escape_string($_POST['user']);
$pass = mysql_real_escape_string($_POST['pass']);
//$remem = mysql_real_escape_string($_POST['remem']);
//$expire=time() + 60 * 60 * 24 * 30;

$result = mysql_query("SELECT * FROM TABLENAME WHERE username = '$user'");
while ($row = mysql_fetch_array($result)) {
if (sha1($user.$pass) == $row['pword']) {
    //setcookie('temp', $row['username']);
    session_start();
    $_SESSION['login'] = 1;
    $_SESSION['uname'] = $row['username'];
    $_SESSION['id'] = $row['id'];
    echo "success";
} 
}
?>

Thanks so much!! 非常感谢!! I can't figure this out and have run multiple javascript checks, none of which gave me an error. 我无法弄清楚,并运行了多个JavaScript检查,但没有一个给我一个错误。

Is the login processing page the value of your form's action attribute? 登录处理页面是表单的action属性的值吗? If so it sounds like the form is being submitted instead of your AJAX being called. 如果是这样,听起来好像是在提交表单,而不是在调用您的AJAX。

Are you testing in a browser where you can see the response from your PHP script? 您是否正在浏览器中进行测试,以查看PHP脚本的响应? Chrome/WebKit have a network tab where you can view the response of XHR (Ajax) requests. Chrome / WebKit有一个网络标签,您可以在其中查看XHR(Ajax)请求的响应。

You could try: 您可以尝试:

$('.mainlogin').submit(function(e) {
    e.preventDefault();

as your anonymous function definition to prevent the from from being submitted in the standard HTML fashion. 作为您的匿名函数定义,以防止以标准HTML格式提交。

I'm guessing there's in error in the Javascript somewhere that's causing the Javascript not to finish, therefore sending the user to the PHP page. 我猜Java语言中的某个错误导致Java语言无法完成,因此将用户发送到PHP页面。

Try checking the browser console for errors. 尝试检查浏览器控制台中是否有错误。

你的PHP连接到数据库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM