简体   繁体   English

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

[英]Uncaught SyntaxError: Unexpected token [

I am making a ajax call to a php file and that php file is not returning me the output. 我正在对一个php文件进行ajax调用,而该php文件未向我返回输出。 Chrome browser is showing the above error. Chrome浏览器显示上述错误。 Loadmessage.php is returning the output but the problem is while fetching. 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);
                                }
                            }
                        });

Here is my loadmessage.php code 这是我的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);
            }
?>

Where should I make the corrections ? 我应该在哪里进行更正?

You have not declared $array anywhere. 您尚未在任何地方声明$array Declare it before the while loop while循环之前声明它

Change this 改变这个

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM