Javascript 错误500(内部服务器错误)ajax和laravel
大家好,我的评论系统在laravel和ajax中遇到了问题。实际上,它只适用于php,但我在使用ajax时遇到了问题 这是错误消息: 状态代码:500内部服务器错误。错误为:1/3 SQLSTATE[23000]:完整性约束冲突:1048列“post_id”不能为null 我在一个模式中编辑评论,我可以创建一个新的评论,但问题是用ajax编辑它 JS代码:Javascript 错误500(内部服务器错误)ajax和laravel,javascript,php,ajax,laravel,Javascript,Php,Ajax,Laravel,大家好,我的评论系统在laravel和ajax中遇到了问题。实际上,它只适用于php,但我在使用ajax时遇到了问题 这是错误消息: 状态代码:500内部服务器错误。错误为:1/3 SQLSTATE[23000]:完整性约束冲突:1048列“post_id”不能为null 我在一个模式中编辑评论,我可以创建一个新的评论,但问题是用ajax编辑它 JS代码: <script> var commentId = 0; var divcomment = null;
<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>
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);
}
最后是在我发布的单一视图“我显示评论的地方”上创建的变量
最新更新:
错误消息:
错误1/3
SQLSTATE[23000]:完整性约束冲突:1048列“post_id”不能为null
错误2/3
SQLSTATE[23000]:完整性约束冲突:1048列“post_id”不能为null
错误3/3
SQLSTATE[23000]:完整性约束冲突:1048列“post\u id”不能为空(SQL:插入注释(注释,已批准,post\u id
,用户id
,在处更新,在处创建)值(另一条注释,2017-06-04:54:342017-06-04:34))
其他信息:
概述
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']);
看来我的直觉是正确的。你有两条相互冲突的路线
Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
当然
Route::post('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
因为这两个路由使用大致相同的路由,所以laravel只需首先定义哪个路由,即您的注释.store
路由
有几种方法可以解决这个问题
更改路线的顺序:
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']);
使用管线约束:
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注册器如何处理Facade方法的外壳(下部、上部)。。因此,为了避免引起更多的bug,我使用了POST
的下框,就像文档中使用的一样。尝试使用save()
而不是update()
更新模型。请附上注释表的架构。Hello@Anton查看上面的更新question@JuanRincón我注意到您的路线地址只是注释/
,而您正在请求注释/更新
。这是一个打字错误,还是可能执行了错误的代码?我建议使用save()
而不是update()
,因为update()
通常使用一个参数;要设置为值的字段数组。我还没有看到它被用在其他方面,但我可能错了。
Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
Route::post('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
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']);
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']);