Ajax成功后,重新加载部分页面,而不是整个页面.拉拉维尔
Reload part of page, not whole page, once Ajax is successful. Laravel
我的项目使用Laravel 4。我有一个帖子的"新闻推送"。每个帖子都可以发表评论。当用户对帖子发表评论时,页面会重新加载,新的评论会显示在评论框中。我多么不希望整个页面重新加载。因此,我可以使用ajax提交帖子。我提交表单并阻止页面刷新。然而,现在我不知道如何在不手动刷新页面的情况下让评论出现在帖子上?
这是我发布评论的路线:
Route::post('post/{id}/comment', ['as' => 'commentPost', 'uses' => 'CommentsController@postComment']);
将此评论发布到DB:的控制器
public function postComment()
{
extract(Input::only('user_id', 'resource_id', 'body'));
$this->execute(new PostCommentCommand($user_id, $resource_id, $body));
return Redirect::back();
}
我使用的命令总线最终将注释保存到DB中,如下所示:
public function leaveComment($user_id, $resource_id, $body)
{
$comment = Comment::leavePostComment($resource_id, $body);
User::findOrFail($user_id)->comments()->save($comment);
return $comment;
}
评论模型:
public static function leavePostComment($resource_id, $body)
{
return new static([
'resource_id' => $resource_id,
'body' => $body
]);
}
这是加载评论框的视图(包含属于帖子的所有评论和用于发布新评论的类型框。当用户点击"输入/返回"键时,我使用JS发布评论):
<div class="comment-box-container">
<div class="comment-box">
@if ($type->comments)
@foreach ($type->comments as $comment)
<div class="user-comment-box">
<div class="user-comment">
<p class="comment">
<!-- starts off with users name in blue followed by their comment-->
<span class="tag-user"><a href="{{ route('profile', $comment->owner->id) }}">{{ $comment->owner->first_name }} {{ $comment->owner->last_name }}</a> </span>{{ $comment->body }}
</p>
<!-- Show when the user posted comments-->
<div class="com-details">
<div class="com-time-container">
{{ $comment->created_at->diffForHumans() }} ·
</div>
</div>
</div><!--user-comment end-->
</div><!--user-comment-box end-->
@endforeach
@endif
<!--type box-->
<div class="type-comment">
<div class="type-box">
{{ Form::open(['data-remote', 'route' => ['commentPost', $id], 'class' => 'comments_create-form']) }}
{{ Form::hidden('user_id', $currentUser->id) }}
{{ Form::hidden($idType, $id) }}
{{--{{ Form::hidden('user_id', $currentUser->id) }}--}}
{{ Form::textarea('body', null, ['class' =>'type-box d-light-solid-bg', 'placeholder' => 'Write a comment...', 'rows' => '1']) }}
{{ Form::close() }}
</div><!--type-box end-->
</div><!--type-comment-->
</div><!--comment-box end-->
这是我的ajax的Javascript:
(function(){
$('form[data-remote]').on('submit', function(e){
var form = $(this);
var method = form.find('input[name="_method"]').val() || 'POST';
var url = form.prop('action');
$.ajax({
type: method,
url: url,
data: form.serialize(),
success: function() {
** I DON'T KNOW WHAT TO PUT HERE TO MAKE THE
COMMENT-BOX RELOAD AND DISPLAY THE NEWLY
POSTED COMMENT **
}
});
e.preventDefault();
});
})();
请注意,我对JavaScript非常陌生,甚至对Ajax也很陌生。任何帮助都将不胜感激!!
类似的东西
$.ajax({
type: method,
url: url,
data: form.serialize(),
success: function(data) {
// use a temp wrapper element to workaround weak jQuery HTML parser
var tmp = $('<div>');
tmp.html(data);
// update the comment box with the new one
$('.comment-box').html(tmp.find('.comment-box').html());
// update the form (so it eventually shows validations errors)
// don't forget to empty it via PHP if the submission was
// successful
$('.type-box').html(tmp.find('.type-box').html());
tmp.destroy();
}
});
相关文章:
- 数据表 AJAX 筛选器重新加载数据
- 如何在新加载的页面上执行(下拉)操作
- 根据上一个选项卡的选择器重新加载选项卡
- 获得“中止,因为不接受 0”并使用反应热加载器重新加载整页
- 咕噜咕噜的手表:实时加载重新加载落后 1 步....
- 如何为新加载的元素提供JQuery UI工具提示
- 如何在新加载的页面上使用相同的JavaScript代码(通过重定向)
- 使用PHP或Javascript检测页面刷新或新加载
- 砌体重新加载&reloadItems不起作用
- 如何检测如果谷歌地图加载成功
- Angularjs表单验证显示页面加载成功
- 如何使javascript计时器重新加载
- 如何获取新加载的图像's width / naturalWidth
- 检测视频.加载成功与否- javascript
- Javascript跨域图像加载检查加载图像是否加载成功
- 在远程URL加载成功后获取内容
- 列出并调用firefox扩展中新加载的选项卡中的js函数
- 查看SWF是否加载成功
- 旋转器重新加载图像似乎在覆盖层后面
- AJAX调用后,新加载的CSS选择器对jQuery.ech()不可用