L
L
LP-DIMAN2015-12-11 15:07:31
JavaScript
LP-DIMAN, 2015-12-11 15:07:31

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));
  
    });
  
    });
});


Here is the method that should handle all this:
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);
  

    
  }

View:
<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

And route:
Route::get('edit_advert','[email protected]_advert');
Route::post('add_comment','[email protected]_advert')


What could be the problem?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Evgeniy Samoilenko, 2015-12-11
@LP-DIMAN

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.

S
Stanislav Pochepko, 2015-12-11
@DJZT

You need to do this in JS

params = $('.add_comment').serialize()
$.post('add_comment', {id_advert: <your id advert>, data: params});

And on the server to accept
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);
  

    
  }

R
romy4, 2015-12-11
@romy4

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

R
Rikcon, 2015-12-11
@Rikcon

take a look at the error.log which is PHP, and take a look at the laravel logs too.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question