简体   繁体   中英

I have got some error ajax

I have an error where I can't proceed my login_process.php using ajax. I have stored all my files as a one folder. If I run my HTML log in page, the error is appeared. Below is my code. It is just a simple code. Before this, I have applied the similar code as below (except for login_process.php because I've put a few functions) as my homework. But when I've try to make it again, as i said, the browser, either in Chrome or Firefox, displays an error as I wrote in ajax below.

index.html(ajax part)

$(document).ready(function(){
        $("#subBTN").click(function(){
            var username = $("#username").val();
            var password = $("#password").val();
            var status = $("#statusAlert");

            if(username === "" && password === ""){
                status.html("<span>Username and password are both required!</span>");
            }
            else if(username === ""){
                status.html("<span>What is your username?</span>");
            }
            else if(password === ""){
                status.html("<span>What is your password?</span>");
            }
            else{
                $.ajax({
                    url:'login_process.php',
                    type: 'post',
                    data: {"?admin_name=": username, "&admin_pass=": password},
                    dataType: 'html',
                    success: function(data){
                        //alert(data);
                        if(data.success){                           
                            status.html("<span>"+ data +"</span>");
                        }
                        else{                               
                            status.html("<span>error</span>");
                            return false;   
                        }
                    }       
                }); 
            }
        });
    });

login_process.php

<?php
session_start();

if($_POST):
    $username = $_POST["username"];
    $password = $_POST["password"];

    if($username):
        echo "Hi " . $username;
    endif;
endif;
?>

You got a error in jQuery ajax call in passing data

You use :

data: {'admin_name':username, 'admin_pass': password},

instead of

data: {"?admin_name=": username, "&admin_pass=": password},

Or access in php code data to use:

$_POST['admin_name'] AND S_POST['admin_pass']

You should change this line of code

                    if(data.success){                           
                        status.html("<span>"+ data +"</span>");
                    }
                    else{                               
                        status.html("<span>error</span>");
                        return false;   
                    }

To

                    if(data.Length > 0){                           
                        status.html("<span>"+ data +"</span>");
                    }
                    else{                               
                        status.html("<span>error</span>");
                        return false;   
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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