2015-04-30 114 views
1

卡住爲什麼這不起作用,任何人有任何想法?它應該更新按鈕,我想這樣做,只是顯示的其他在結束所有的時間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"; 
    } 

}  

它的意思回顯正確的錯誤信息,但沒有。也永遠不會發送好友請求發送消息

+0

顯然'responseText'既不是'friend_request_sent'也不是'unfriend_ok'。不確定你想要我們做什麼。如果您確實發回了正確的回覆,請提供有關此問題的更多信息。否則,請更新您的客戶端代碼以匹配您從服務器獲得的響應,或者更新您的服務器端代碼以返回正確的響應。 –

+0

在你的if(this.responseText ==「friend_request_sent」)之前加上alert(this.responseText);'並且比較你的結果! – GeoffreyB

+0

Geoffrey,它沒有效果,只是提出了一個空白的警報框,沒有響應文本,即使這個人不是朋友 – user1886385

回答

0

您的內容長度不正確。它被設置爲user的長度,但是您發送的數據比POST數據中的多。看起來Web服務器正在將您的數據縮減到錯誤的長度,然後無法分析格式錯誤的字符串。

讓你的POST數據到一個變量,這樣你可以得到的長度,而無需複製它:

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

然後當你調用發送:

request.send(data); 

邊注:添加encodeURIComponent正確編碼這些值。

+0

在調試中得到這個錯誤 拒絕設置不安全的標題「內容長度」 拒絕設置不安全的標題「連接」 – user1886385

+0

在PHP中,if($ _ POST ['type'] ==「friend」)'行前,執行'var_dump($ _ POST);'來查看接收到的內容。 – MrCode

+0

POST https://dunluce.infc.ulst.ac.uk/B00677090/com302/GroupAssignment/friendSystem.php \t 200 OK \t \t \t爲35ms的jquery -...>的eval(線140) HeadersPostHTMLCookies Parametersapplication/X-WWW的形式urlencodedDo不是那種 型\t 朋友 用戶\t LyndsRob2 來源 類型=朋友和用戶= LyndsRob2 – user1886385