Answer the question
In order to leave comments, you need to log in
Why does a 500 Internal Server Error pop up when trying to send an ajax request to the server using the post method?
I get records of a certain user from the database. If desired, the user can leave a comment on the entry. There is a "Add comment" button, when clicked, a textarea with the "Add" button falls out. That's all I want to do with ajaxom. I get the text from the textarea, but the whole thing does not get into the database.
Here is the script:
$('.comment').click(function(){
var id_advert = $(this).val();
//console.log(id_advert);
$(this).html("<textarea rows=10 cols=70 name='add_comment' class='add_comment' maxlengh='256' autofocus> </textarea><br><button type='submit' name='add' class='add'> Добавить сообщение</button>");
$.get('edit_advert',{comment:id_advert},function()
{
$('.add').click(function(){
var params = $('.add_comment').serialize();
console.log(params);
console.log($.post('add_comment',params));
});
});
});
public function edit_advert(Request $request)
{
$id_advert = $_GET['comment'];
$id_client = Auth::user()->id;
$comment = $request->input('add_comment');
Adverts::add_comment($comment,$id_client,$id_advert);
}
<div class="panel-body">
@foreach ($remember_adverts_client as $advert)
<div class="table table-bordered">
Объявление добавлено <em> {{$advert->date}} </em> <br>
<strong>{{$advert->title}}</strong><br>
<strong>Тип недвижимости: </strong>{{$advert->type}}<br>
<strong>Количество комнат: </strong>{{$advert->quantity_room}}<br>
<strong>Город: </strong>{{$advert->city}}<br>
<strong> Описание: </strong> {{$advert->description}}<br>
<strong> Телефон: </strong>{{$advert->phone}}<br>
<!--<form action="edit_advert" method="GET"> -->
<button type="submit" value="{{$advert->id_realty}}" name="comment" class="comment"> Добавить комментарий</button>
<button type="submit" value="{{$advert->id_realty}}" name="cross"> Перечеркнуть </button>
<button type="submit" value="{{$advert->id_realty}}" name="lead">Обвести</button>
<button type="submit" value="{{$advert->id_realty}}" name="link">Поделиться ссылкой</button>
<!--</form> -->
@endforeach
Route::get('edit_advert','[email protected]_advert');
Route::post('add_comment','[email protected]_advert')
Answer the question
In order to leave comments, you need to log in
Add token
into a request to the server and everything will be ok, or change the type from post to get.
It does not accept because it costs csrt protection, it helps to protect itself from request spoofing.
And a New Year's gift - the error can be viewed in the browser console, everything is intuitively clear there.
You need to do this in JS
params = $('.add_comment').serialize()
$.post('add_comment', {id_advert: <your id advert>, data: params});
public function add_comment_advert(Request $request)
{
$id_advert = $request->input('id_advert');
$id_client = Auth::user()->id;
$comment = $request->input('data.add_comment');
Adverts::add_comment($comment,$id_client,$id_advert);
}
It's not about ajax, it's about the receiving side. Either htaccess is configured incorrectly, or the script crashes, and the backend crash is always 500
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question