未捕获的SyntaxError:意外的令牌[

时间:2014-10-31 07:50:44

标签: php ajax

我正在对一个php文件进行ajax调用,并且该php文件没有返回输出。 Chrome浏览器显示上述错误。 Loadmessage.php正在返回输出但问题是在获取时。

                       $.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);
            }
?>

我应该在哪里进行更正?

1 个答案:

答案 0 :(得分: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);