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

时间:2015-01-02 10:50:01

标签: php jquery ajax post

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

现在我正在尝试使用jquery ajax验证用户名是否已存在。 jquery代码运行但仍显示“正在检查可用性”。

这是我用过的代码。请忽略我的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">&nbsp;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请求发送到该文件?

3 个答案:

答案 0 :(得分:0)

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

 - 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

答案 1 :(得分:0)

你混淆了ajax函数......语法将是这样的

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

    },
    success: function (returnData) {

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

    },
    complete: function () {

    }
});

答案 2 :(得分:0)

你做对了。检查它们的方法很简单就是在你的浏览器上使用firebug工具......我建议首先使用firebug.install安装firefox,然后在发布表单之前将其打开。然后转到控制台日志并发送表单。 ..检查出来,最好的软件。