我遇到了返回数据的问题。 Ajax调用error
然后调用success
。 success
除了空数组外不会返回任何内容。它不是服务器端代码问题,我测试了代码并且完美无缺。
这是我的jquery代码:
$(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;
}
});
}
});
这是我的PHP代码:
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 }
}
捕获:
我试过了:
FormData
。取而代之的是data:{op: "5623", offset: msgs},