简体   繁体   English

AJAX成功或错误未触发

[英]AJAX Success or error not firing

I can't seem to figure out why this is not working or find a relevant article. 我似乎无法弄清楚为什么它不起作用或找不到相关文章。 I can see the json object in firebug return either success: false or success: true from the post request so i don't know why the function won't fire. 我可以从发布请求中看到firebug中的json对象返回成功:false或成功:true,所以我不知道为什么函数不会触发。

ajax 阿贾克斯

$("#login").submit(function (e) {
    e.preventDefault();

    $.ajax({
        url: "../process/login-process.php",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: { 'username': $('[name=username]').val(), 'password': $('[name=password]').val() },
        beforeSend : function (){
            //$("#login").hide();       
        },
        success: function(data){
            window.localation=data.redirect;            
        },
        error: function(data) {
            $(".error").html('');
            alert('wtf');
            if(data.empty) {
                $(".error").html(data.empty);
            }
            if(data.incorrect) {
                $(".error").html(data.incorrect);
            }       
        }
    });
});

php PHP

<?php 
    session_start();
    include "../inc/connect.php";
    $username = mysqli_real_escape_string($con, $_POST['username']); 
    $password = mysqli_real_escape_string($con, $_POST['password']); 
    $password = hash('sha256', $password); 
    $sql = "SELECT * FROM admin WHERE username ='$username' AND password ='$password'";
    $result = mysqli_query($con, $sql) or die(mysqli_error($con));
    $row = mysqli_fetch_array($result);
    $count=mysqli_num_rows($result); 
    if ($username == "" || $password == "") {
        $data['empty'] = 'All fields are required.';
    } else if(($count==1) && ($username)) {
        $_SESSION['user'] = $row['firstName']; 
        $_SESSION['userID'] = $row['adminID'];
        $data['success'] = true;
        $data['redirect'] = '../pages/dashboard.php';
     } else { 
        $data['success'] = false;
        $data['incorrect'] = "Incorrect Username or Password.";
     } 
    echo json_encode($data);
?> 

There are two things which you need to correct in your code, 您需要在代码中纠正两件事,

  1. change window.localation to window.location in success callback success回调中将window.localation更改为window.location
  2. Add dataType: 'JSON' in ajax request or use $.parseJSON in success callback 在ajax请求中添加dataType: 'JSON'或在成功回调中使用$.parseJSON

So, your success callback should be: 因此,您的成功回调应为:

success: function(data){
            data = $.parseJSON(data); // <- Don't use this if you add dataType: 'JSON' in ajax
            window.location=data.redirect;        //<- Spelling corrected for location    
        }

you are sending a string(in this case, a json) without any header mentioned. 您正在发送未提及任何标头的字符串(在本例中为json)。 so it will always be 200 ok. 所以永远是200 OK。

hence, error will never fire. 因此,错误永远不会触发。

success: function(data){
        window.localation=data.redirect;   <----- error here. location         
    },

that's why nothing is happening. 这就是为什么什么都没有发生的原因。

to fire the error in case data is incorrect 在数据不正确的情况下触发错误

instead of: 代替:

} else { 
    $data['success'] = false;
    $data['incorrect'] = "Incorrect Username or Password.";
 } 

use 采用

} else { 
    header('HTTP/1.0 403 Forbidden');
    echo 'Incorrect Username or Password.';
    exit;
 } 

this will give you a 403 forbidden message. 这会给您一条403禁止消息。

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

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