Ajax调用没有返回预期值

时间:2015-04-30 15:47:42

标签: javascript ajax

坚持为什么这不起作用,任何人都有任何想法?它应该更新按钮,我认为它确实如此,只是一直在最后显示其他

    function friendToggle(type,user,elem){
    var conf = confirm("Press Ok to confirm the '" +type+ "' action for user " + user);
    if(conf != true){
        return false;
    }

    O(elem).innerHTML = 'please wait...';

    params = user
    request = new ajaxRequest()
    request.open("POST", "friendSystem.php", true)
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
    request.setRequestHeader("Content-length", params.length)
    request.setRequestHeader("Connection", "close")

    request.onreadystatechange = function(){
        if(this.readyState == 4){
            if(this.responseText == "friend_request_sent"){
                O(elem).innerHTML = 'OK Friend Request Sent';
            }else if(this.responseText == "unfriend_ok"){
                O(elem).innerHTML = '<button onclick="friendToggle(\'friend\',\'echo $view;\',\'friendBtn\')">Request As Friend</button>';
            }else {
                alert(request.responseText);
                O(elem).innerHTML = 'Try again later';
            }
        }   
    }   
request.send("type="+type+"&user="+user);

}

这是它所调用的代码:

if($_POST['type'] == "friend")
{
    $friendCount = mysqli_fetch_row(querySQL("SELECT COUNT(*) FROM friends WHERE user='$user' AND accepted='1' OR friend='$user' AND accepted='1'"));

    $blockCount1 = mysqli_fetch_row(querySQL("SELECT COUNT(*) FROM blockedusers WHERE blocker='$user' AND blockee='$view' LIMIT 1"));

    $blockCount2 = mysqli_fetch_row(querySQL("SELECT COUNT(*) FROM blockedusers WHERE blocker='$view' AND blockee='$user' LIMIT 1"));

    $rowCount1 = mysqli_fetch_row(querySQL("SELECT COUNT(*) FROM friends WHERE user='$view' AND friend='$user' AND accepted='1' LIMIT 1"));

    $rowCount2 = mysqli_fetch_row(querySQL("SELECT COUNT(*) FROM friends WHERE user='$user' AND friend='$view' AND accepted='1' LIMIT 1"));

    $rowCount3 = mysqli_fetch_row(querySQL("SELECT COUNT(*) FROM friends WHERE user='$view' AND friend='$user' AND accepted='0' LIMIT 1"));

    $rowCount4 = mysqli_fetch_row(querySQL("SELECT COUNT(*) FROM friends WHERE user='$user' AND friend='$view' AND accepted='0' LIMIT 1"));

    if($friendCount[0] > 99){
        echo"$user currently has the maximum number of friends and cannot accept any more";
    }elseif($blockCount1[0] > 0){
        echo "$user has you blocked, we cannot proceed.";
    }elseif($blockCount2[0] > 0){
        echo"You must first unblock $user in order to friend them.";
    }elseif($rowCount1[0] >0 || $rowCount2[0] > 0){
        echo"You are already friends with $user.";
    }elseif($rowCount3[0] >0){
        echo"You have a pending friend request already sent to $user.";
    }elseif($rowCount4[0] > 0){
        echo"$user has requested to friend with you first. Check your friend requests";
    }else{
        querySQL("INSERT INTO friends VALUES('$user','$view','0')");
        echo"friend_request_sent";
    }

}       

它的意思是回应正确的错误信息,但不是。也永远不会发送好友请求发送消息

1 个答案:

答案 0 :(得分:0)

您的内容长度不对。它设置为user的长度,但您发送的数据多于POST数据。似乎Web服务器正在将数据削减到错误的长度,然后无法解析格式错误的字符串。

将您的POST数据转换为变量,这样您就可以获得长度而无需复制它:

var data = "type="+encodeURIComponent(type)+"&user="+encodeURIComponent(user);
request.setRequestHeader("Content-length", data.length);

然后当你致电发送时:

request.send(data);

附注:添加encodeURIComponent以正确编码值。