Ajax返回错误,然后返回成功,返回一部分数据

I'm having a issue with returning data. Ajax calls error and then success. success doesn't return everything but an empty array. It's not a server side code issue, I tested the code and works perfect without any errors.

This is my jquery code:

$(document).on('click', '#paginate',function(){

    if(loadmsg===true){
        loadmsg=true;

        var data=new FormData();
        data.append("op", "5623");
        data.append("offset", msgs);

        $.ajax({
            url:"ajax/scripts/message.php?w=<?php echo $workspace_id?>",
            type:'POST',
            contentType: false,
            processData: false,
            data:data,
            error:alert("something went wrong!"),
            success:function(data){
                alert(data);
                $('#message_board').prepend(data);
                msgs=msgs+20;
                loadmsg=true;
                }
            });
        }   
    });

And this is my php code:

if($user_id != "" && isset($_GET['w']) && isset($_POST['op']) && $_POST['op']==5623 && isset($_POST['offset'])){
    $workspace_id=isset($_POST['w']) ? $_POST['w'] : "";
    $offset=$_POST['offset'];

    try{$sql="SELECT messages.*, users.first_name, users.last_name FROM messages 
        LEFT JOIN users ON users.user_id=sent_by 
        WHERE workspace_id=:w_id 
        ORDER BY message_created DESC
        LIMIT 20 OFFSET :offset";
        $stmt=$db->prepare($sql);
        $stmt->bindValue(":w_id", $workspace_id, PDO::PARAM_INT);
        $stmt->bindValue(":offset", (int)$offset, PDO::PARAM_INT);
        $stmt->execute();
        $messages=$stmt->fetchAll(PDO::FETCH_ASSOC);
        }catch(Exception $e){echo $e->getMessage();}

echo"Test";
echo "<pre>";
print_r($messages);
echo "<pre>";

  foreach($messages as $messages){?>
  <tr>
    <td>
    <?php echo $messages['first_name'] . " " . substr($messages['last_name'], 0 ,1) . "."; ?>
    </td>
    <td>
    <?php echo $messages['message'];  ?><br>
    <?php if($messages['attachment_id'] == "1"){
        try{
            $sql="SELECT * FROM files WHERE message_id = {$messages['message_id']} ORDER BY file_id DESC";
            $files=$db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
            }catch(exception $e){echo $e->getMessage();}

            echo "<div class='attach-list'>";

        foreach($files as $files){
            echo "<li>
            <a href='../../users/".$user_id."/workspace/".$workspace_id."/" . $files['file_name'] . "'>". $files['file_name'] ."</a>
            </li>";
            } 
            echo "</div>";
        } ?>
    </td>
    <td>
    <?php 
      $date=$messages['message_created'];
      echo date("M d, Y", strtotime($date)) . "<br>";
      echo date("h:i", strtotime($date));
    ?></td>
  </tr>
  <?php }   
 }

Capture:

enter image description here

I've tried:

  • Not using FormData. Instead data:{op: "5623", offset: msgs},
  • Commenting out contentType and processData