Php Ajax返回错误,然后成功返回部分数据
我在返回数据时遇到问题。Ajax调用error,然后调用success。成功不会返回所有内容,只会返回一个空数组。这不是服务器端的代码问题,我测试了代码,没有任何错误 这是我的jquery代码:Php Ajax返回错误,然后成功返回部分数据,php,jquery,ajax,Php,Jquery,Ajax,我在返回数据时遇到问题。Ajax调用error,然后调用success。成功不会返回所有内容,只会返回一个空数组。这不是服务器端的代码问题,我测试了代码,没有任何错误 这是我的jquery代码: $(document).on('click', '#paginate',function(){ if(loadmsg===true){ loadmsg=true; var data=new FormData(); data.append("op
$(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。而是数据:{op:5623,offset:msgs},
注释掉contentType和processData
您是否尝试过包装警报。。您在包装器函数中使用错误?functionresponse,status,error{alert…}@user3154108尝试了您所说的。现在我没有收到任何错误,但success仍然不会在foreach循环中返回任何内容:尝试不要将$messages用作$messages,而是将$messages用作$message,这样循环变量就不同于列表变量。请将要作为响应返回的HTML数据存储在不同的变量中。然后最后回显该变量并退出它。避免使用多个echo语句。