dqndc26628 2014-10-31 07:50
浏览 104

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

I am making a ajax call to a php file and that php file is not returning me the output. Chrome browser is showing the above error. Loadmessage.php is returning the output but the problem is while fetching.

                       $.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

<?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 ?

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-10-31 07:59
    关注

    You have not declared $array anywhere. Declare it before the while loop

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度