2014-10-31 161 views
0

我正在對ajax調用一個php文件,並且該php文件沒有返回給我輸出。 Chrome瀏覽器顯示上述錯誤。 Loadmessage.php正在返回輸出,但問題在於提取。未捕獲的SyntaxError:意外的令牌[

     $.ajax({ 

          url: "loadmessage.php", 
          type: "POST", 
          data:{ 
           'sender': sender, 
           'receiver': receiver, 
          }, 

          success: function(response){ 
           var result = JSON.parse(response); //Chrome showing error over here 
           console.log("Result is " +result); 
           console.log() 

           for(var i in result){ 
            $("#m-"+id).append('<p class = "shout_msg">' +result[i]+ '</p>'); 
            $("#m-"+id).scrollTop($("#m-"+id)[0].scrollHeight); 
           } 
          } 
         }); 

這裏是我的loadmessage.php代碼

<?php 
$sender_id = 0; 
$receiver_id = 0; 
session_start(); 
    if(isset($_SESSION['login_user'])) { 
    } 
    else { 
     header('location: ChatLog.php'); 
    } 

    if(isset($_REQUEST['sender']) AND isset($_REQUEST['receiver'])){ 

     $sender = $_REQUEST['sender']; 
     $receiver = $_REQUEST['receiver']; 

      require_once 'dc_chat.php'; 
      $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); 

      $result = $mysqli -> query("SELECT id from users where username LIKE '{$sender}'"); 
      $row = mysqli_fetch_row($result); 
      $sender_id = $row[0]; 
      echo $sender_id; 

      $result = $mysqli -> query("SELECT id from users where username LIKE '{$receiver}'"); 
      $row = mysqli_fetch_row($result); 
      $receiver_id = $row[0]; 

      $sql = $mysqli -> query("SELECT * from messagse where sender_id = $sender_id AND receiver_id = $receiver_id");    

       while($row = mysqli_fetch_array($sql)) { 
        $array[] = $row[3].": ".$row[4]; 
       } 
        echo json_encode($array); 
      } 
?> 

我應該在哪裏進行更正?

+0

嘗試添加空間AFER'<?php'並請刪除'?>'。你不需要那 – Umair 2014-10-31 07:58:02

回答

0

您還沒有在任何地方聲明$array。在while

改變之前聲明它這個

while($row = mysqli_fetch_array($sql)) { 
       $array[] = $row[3].": ".$row[4]; 
      } 
       echo json_encode($array); 

$arr = array(); 
    while($row = mysqli_fetch_array($sql)) { 
        $arr[] = $row[3].": ".$row[4]; 
       } 
        echo json_encode($arr); 
+0

我做了同樣的,但仍然給我同樣的錯誤。 – 2014-10-31 08:07:27

+0

它說錯誤的行號是什麼? – Umair 2014-10-31 08:08:59

+0

此處顯示的錯誤 var result = JSON.parse(response); – 2014-10-31 08:30:52

相關問題