E
E
Etmac2016-07-01 22:57:31
Laravel
Etmac, 2016-07-01 22:57:31

Why is there a 500 error when sending a request via ajax?

I decided to try to create a record dynamically using ajax , it turned out, but with crutches, it seems to me. Next, I wanted to pull up the validation for the record, but at this point I got a POST localhost:8000/create 500 (Internal Server Error) error . By the way, before validation, it already seemed to me that my code was already working somehow strangely, for example, in a controller method, at the beginning I wrote exit('works')to just check if a request is being made to my url, it used to work - the script just ended with this message, but now the message was displayed in the console with the .done() method, in theory the code should have reached the controller and the script should have been executed there- and no matter how strange it seems, don’t be angry if it’s obvious to you :) Plus, in .done() I add data that came in json to the tags, if I don’t write this, then the data appears only when the page is refreshed, with Is this all right or not?)
Googling a little, I found solutions, basically everyone wrote about the fact that you need to pass the token in the headers like this:

$.ajaxSetup({
                 headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

But it did not help , I tried to look at my logs /var/log/apache2/error.log - I did not find anything military. Most likely I'm using ajax somehow wrong. Here is the whole code
$(document).ready(function() {
            $(".interaction").find("#create").on("click", function() {
                 $("#create-modal").modal();
            });

            $('#modal-save').on('click', function() {

                var token = '{{ csrf_token() }}';
                var url = '{{ route('create.post') }}';
                $.ajaxSetup({
                     headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                $.ajax({
                    method: 'POST',
                    url: url,
                    data: {header: $("#post-header").val(), body: $("#post-body").val(), _token: token},
                   
                }).done(function (msg) {
                     console.log(JSON.stringify(msg));
                    $("#posts").append("<h2>" + msg.title +"</h2>");
                    $("#posts").append("<p>" + msg.body + "</p>");
                    $("#create-modal").modal('hide');

                }).fail(function( jqXHR ) {
                    if (jqXHR.status == 422) 
                    {
                        console.log('It doestn't works');
                    }
                });
             });
        });

All this is wrapped in @section('content') and inherits my master template , where in the header I include scripts for this
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


Route::post('/create', ['uses' => '[email protected]', 'as' => 'create.post']);
- I send everything to this URL, and in the createPost method I write the following:
$post = new Post;

   $validator = Validator::make($request->all(), [
      'header' => 'required|unique:posts|min:5|max:100',
      'body'  => 'required|max:200'
    ]);

   if ($validator->fails()) {
      return response()->json(['errors' => $errors], 422);
   }
   $post->title = $request['header'];
   $post->post_body = $request['body'];
   $post->user_id = 1;
   $post->save();

    return response()->json([
      'title' => $post->title,
      'body' => $post->post_body,
      ]);


For whom this information is not enough, if anything, here is a link to the entire project https://www.dropbox.com/sh/zufx3bhusbjhni3/AADzrOF... .
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-07-07
@bitw

look at the text of the error in storage/logs/laravel.log it is
quite possible that _method: 'method' must also be passed to the server with the data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question