如何知道php文件是否处理ajax请求


how to know whether php file handling the ajax request or not

我有以下ajax请求的代码片段:

<script type="text/javascript">
        $(document).ready(function() {
            $('#temp').click(function() {
                var username = $('#un').val();
                $.ajax({
                    url: "inter.php",
                    data: 'user=' + username,
                    cache: false,
                    type: 'GET',
                    success: function(data, textStatus, jqXHR) {
                        if (! data.length === 0) {
                            $('#msg').append('available');
                            $('#sub').removeAttr('disabled');
                        } else {
                            $('#msg').append("Username not available");
                            $('#sub').attr('disabled', 'true');
                        }
                    }
                });
            });
        });
    </script>

以及inter.php中的代码

<?php
$arr = array('one', 'two', 'three');
if (in_array($_GET['user'], $arr)) {
    echo "Username available";
} else {
    //echo "0";
}
?>

但是,每次我收到消息Username not available
有人能帮忙吗?我是ajax的新手。

编辑
$_GET['user']
但同样的问题仍然存在。

问题:

data: 'user=' + username,
$_GET['un']

对于"用户名"或其他内容,您没有使用相同的参数名称。在JS中,您称之为"user",在PHP中,您要查找"un"。

您正在发送?user=someName,但正在查找$_GET['un']。将其更改为$_GET['user']

为了调试我添加的代码行:

alert(data + " " + data.length);

之后

success: function(data, textStatus, jqXHR) {

发现我从php文件中得到了正确的消息
然后我发现问题出在我的处理程序上

if(!data.length === 0){

其中CCD_ 7运算符只对CCD_
我把它改成了

if(!(data.length === 0)){

现在它起作用了。