我有以下ajax代码:
$.ajax({
type: 'post',
url: '/conversation/test',
data: { conversation: JSON.stringify(<?php echo json_encode($conversation) ?>) },
success: function (response) {
console.log(response);
}
});
现在我的test.php:
<?php
$conversation = json_decode($_POST['conversation']);
?>
<?php foreach ($conversation->getUsers() as $conUser) {
// Skip current user
if ($conUser->getId() == UserSession::getUserId()) {
continue;
} ?>
<a href="/<?php echo $conUser->getUri(); ?>/"><?php echo $conUser->getName(); ?></a>
<?php } ?>
我在控制台上的回答是:
<br />
<b>Fatal error</b>: Call to undefined method stdClass::getUsers() in <b>/Users/msalamanca/PhpStorm/pinporn/trunk/application/views/default/conversation/test.php</b> on line <b>8</b><br />
我不明白我在这里做错了什么。
答案 0 :(得分:1)
最后我理解我做错了什么。所以在我看来,我做了以下几点:
<div id="messages-widget">
</div>
<!-- Updates the conversation display and check for new messages -->
<script>
$(document).ready(function () {
$.post( "/conversation/messages", { conId: <?php echo $conversation->getId(); ?> })
.done(function (data) {
$('#messages-widget').html(data);
});
});
</script>
然后在我的控制器中我刚刚做了以下事情:
public function messages($args=array()) {
$conId = $args['conId'];
// Get current conversation
$conversation = ConversationModel::getConversationById($conId);
DataHolder::getInstance()->addObject('conversation', $conversation);
}
所以像这样,我的视图将拥有我想要的对话对象及其属性。这样做,我可以使用我需要的getter和setter。