如何检查 jquery ajax 是否发送 POST 请求?

How to check if jquery ajax send POST request or not?

我使用 PHP 创建了一个简单的登录注册程序。

现在我正在尝试使用 jquery ajax 验证用户名是否已经存在。 jquery 代码运行但继续显示 'Checking Availability'。

这是我用过的代码。请忽略我的 PHP 代码中的漏洞和其他错误(可能不会影响 jquery ajax 进程),因为我是新手。我正在努力改进这些东西。

Register.php

<?php
include('config.php');
if(isset($login_session))
{
header("Location: login.php");
}
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
$username = mysqli_real_escape_string($obj->conn,$_POST['username']); 
$password = mysqli_real_escape_string($obj->conn,$_POST['password']); 
$name     = mysqli_real_escape_string($obj->conn,$_POST['name']); 
$email    = mysqli_real_escape_string($obj->conn,$_POST['email']); 

$password = md5($password);

$sql ="SELECT uid from users WHERE username = '$username' or email = '$email'";
$register_user = mysqli_query($obj->conn,$sql) or die(mysqli_error($sql));
$no_rows = mysqli_num_rows($register_user);

if($no_rows == 0)
{
    $sql2 = "INSERT INTO users(username, password, name, email) values ('$username', '$password', '$name', '$email')";
    $result = mysqli_query($obj->conn, $sql2) or die(mysqli_error($sql2));
    echo "Registration Successfull!";
}
else{
    echo "Registration Failed.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/username.js"></script>
</head>
<body>
    <form action="register.php" method="post">
    <label>UserName:</label>
    <input type="text" id="username" name="username" required/>
    <span id="status"></span><br />
    <label>Password :</label>
    <input type="password" name="password" required/><br/>
    <label>Full Name :</label>
    <input type="text" name="name" required/><br/>
    <label>Email :</label>
    <input type="email" name="email" required/><br/>
    <input type="submit" value=" Submit "/><br />
    </form>
</body>
</html>

username.js

    $(document).ready(function()
{
$("#username").change(function() 
{ 

var username = $("#username").val();
var msgbox = $("#status");


if(username.length > 3)
{
$("#status").html('<img src="img/loader.gif" align="absmiddle"> Checking availability...');

$.ajax({  
    type: "POST",  
    url: "php/username-check.php",  
    data: "username="+ username,  
    success: function(msg){  

   $("#status").ajaxComplete(function(event, request){ 

    if(msg == 'OK')
    { 
        msgbox.html('<img src="img/yes.png" align="absmiddle"> <font color="Green"> Available </font>  ');
    }  
    else  
    {  
         $("#username").removeClass("green");
         $("#username").addClass("red");
        msgbox.html(msg);
    }  

   });
   } 

  }); 

}
else
{
$("#status").html('<font color="#cc0000">Enter valid User Name</font>');
}



return false;
});

});

用户名-check.php

    <?php 

include("config.php");

if(isSet($_POST['username']))
{
$username = $_POST['username'];
$username = mysqli_real_escape_string($obj->conn,$username);
$sql = "SELECT username FROM users WHERE username='$username'";
$sql_check = mysqli_query($obj->conn,$sql);

if (!$sql_check)))
{
    echo 'could not complete query: ' . mysqli_error($obj->conn,$sql_check);
}else{
    echo 'query successful!';
}

if(mysqli_num_rows($obj->conn,$sql_check))
{
echo '<font color="#cc0000"><b>'.$username.'</b> is already in use.</font>';
}
else
{
echo 'OK';
}
}
?>

我想知道是否有办法检查 jQuery Ajax 是否向该文件发送了 POST 请求?

使用浏览器实用程序检查请求

 - Launch the chrome browser 
 - Right click and select inspect element menu
 - click on Network tab
 - Load your URL
 - Perform the Ajax request
 - You can see the request here (new request will be last in the list).
 - Click on it 
 - Right side window shows you request and response data

您正在混淆 ajax 函数...语法将像这样

$.ajax({
    url: url,
    data: data, 
    type: "POST",
    beforeSend: function () {

    },
    success: function (returnData) {

    },
    error: function (xhr, ajaxOptions, thrownError) {

    },
    complete: function () {

    }
});

你 correct.Easy 检查它们的方法是在你的浏览器上使用 firebug 工具...我建议先使用 firebug.install 的 firefox,然后再打开它 post 你的 form.then 转到控制台日志并发送你的表单...检查一下,最好的软件。