问题描述
大家好,我在laravel和ajax中的评论系统遇到问题。实际上它只适用于PHP,但我遇到了ajax问题。
Hello people I am facing a problem with my comments system in laravel and ajax. Actually it works only with php but i'm having problems with ajax.
这是错误信息:
I我正在编辑一个模态中的注释,我可以创建一个新注释,但问题是用ajax编辑它。
I am editing the comments in a modal, I can create a new comment but the problem is editing it with ajax.
JS代码:
<script>
var commentId = 0;
var divcomment = null;
$('.edit-comment').click(function(event){
event.preventDefault();
var divcomment = this.parentNode.parentNode;
commentId = $("#comment-post", event.target.parentNode.parentNode).data('commentid');
var commentBody = $(divcomment).find('#display-comment').text();
$('#comment').val(commentBody);
$('#edit-comment').modal();
});
$('#modal-save').click(function(){
$.ajax({
method: 'POST',
url: urlEdit,
data: {
comment: $('#comment').val(),
commentId: commentId,
_token: token,
_method: 'POST'
}
})
.done(function (msg){
$(divcomment).text(msg['new_comment']);
$('#edit-comment').modal('hide');
});
});
</script>
这是HTML:
<article class="row">
<div class="col-md-3 col-sm-3 hidden-xs">
<figure class="thumbnail">
<img class="img-responsive" src="/uploads/avatars/{{ $comment->user->profilepic }}" />
<figcaption class="text-center">{{ $comment->user->name }}</figcaption>
</figure>
</div>
<div class="col-md-8 col-sm-8">
<div class="panel panel-default arrow left">
<div class="panel-body">
<header class="text-left">
<div class="comment-user"><i class="fa fa-user"></i> {{ $comment->user->name }}</div>
<time class="comment-date" datetime="{{ $comment->created_at->diffForHumans() }}"><i class="fa fa-clock-o"></i> {{ $comment->created_at->diffForHumans() }}</time>
</header>
<div id="comment-post" data-commentid="{{ $comment->id }}">
<p id="display-comment">{{ $comment->comment }}</p>
</div>
</div>
<div class="panel-footer list-inline comment-footer">
@if(Auth::guest())
No puedes responder ningún comentario si no has ingresado.
@else
@if(Auth::user() == $comment->user)
<a href="#" data-toggle="modal" data-target="edit-comment" class="edit-comment">Editar</a> <a href="#" data-toggle="modal" data-target="delete-comment" class="delete-comment">Eliminar</a>
@endif
@if(Auth::user() != $comment->user)
<a href="#">Responder</a>
@endif
@endif
</div>
</div>
</div>
</article>
我的编辑模式:
<div class="modal fade" id="edit-comment" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" style="color:#000;">Editar Comentario</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="comment">Editar comentario</label>
<textarea class="form-control" name="comment" id="comment"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn-comment-dismiss btn-comment-modal" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cerrar</button>
<button type="button" class="btn-comment-edit btn-comment-modal" id="modal-save"><span class="glyphicon glyphicon-ok"></span> Editar</button>
</div>
</div>
</div>
</div>
我的评论更新路线:
Route::POST('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
我对CommentsController的更新功能:
My update function on CommentsController:
public function update(Request $request)
{
$this->validate($request, [
'comment' => 'required'
]);
$comment = Comment::find($request['commentId']);
if (Auth::user() != $comment->user) {
return redirect()->back();
}
$comment->comment = $request['comment'];
$comment->update();
return response()->json(['new_comment' => $comment->comment], 200);
}
最后在我的Post单视图上创建的变量where我显示评论
And finally the variables created on my Post single view "where i display the comments"
<script>
var token = '{{ Session::token() }}';
var urlEdit = '{{ url('comments/update') }}';
</script>
更新:
评论表格方案:
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->text('comment');
$table->boolean('approved');
$table->integer('post_id')->unsigned();
$table->timestamps();
});
Schema::table('comments', function ($table){
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});
}
新更新:
错误消息:
错误1/3
错误2/3
错误3/3
其他信息:
一般
Request URL:http://devmedia.dev/comments/update
Request Method:POST
Status Code:500 Internal Server Error
Remote Address:127.0.0.1:80
Referrer Policy:no-referrer-when-downgrade
表格
comment:Another yet comment
commentId:13
_token:Do1gqYfziHij1nAj2CFOWwgdt7UWuubqbawrD5uX
_method:POST
整条评论路线:
Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
Route::get('comments/{id}/edit', ['uses' => 'CommentsController@edit', 'as' => 'comments.edit']);
Route::POST('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
Route::delete('comments/{id}', ['uses' => 'CommentsController@destroy', 'as' => 'comments.destroy']);
Route::get('comments/{id}/delete', ['uses' => 'CommentsController@delete', 'as' => 'comments.delete']);
推荐答案
看来我的(和)预感是正确的。你有两条有冲突的路线。
It appears my(and Anton's) hunch was correct. You have two conflicting routes.
Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
当然
Route::post('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
因为这两条路线大致使用相同的路线,所以laravel就是首先定义的,这是你的 comments.store
路线。
Because the two routes use roughly the same route, laravel just goes by which is defined first, which is your comments.store
route.
有几种方法可以解决这个问题。
There are a couple ways to fix this.
-
更改路线的顺序:
Change the order of your routes:
Route::post('comments/update', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
Route::get('comments/{id}/edit', ['uses' => 'CommentsController@edit', 'as' => 'comments.edit']);
使用路线限制:
Use route constraints:
Route::post('comments/{post_id}', [
'uses' => 'CommentsController@store',
'as' => 'comments.store'
])->where(['post_id' => '[0-9]+']);;
Route::get('comments/{id}/edit', ['uses' => 'CommentsController@edit', 'as' => 'comments.edit']);
Route::post('comments/update', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
值得注意的是,我不知道如何Facade注册商处理外观方法的外壳(下部,上部)。因此,为了不引起进一步的错误,我使用 POST
的下部外壳,就像它一样在文档中使用。
Of note, I don't know how the Facade registrar handles the casing(lower, upper) of facade methods.. So in an effort to not cause further bugs, I used the lower casing of POST
, just as it is used in the documentation.
这篇关于错误500(内部服务器错误)ajax和laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!