L
L
lemonlimelike2018-01-28 22:32:25
Laravel
lemonlimelike, 2018-01-28 22:32:25

Why is ajax request not working in laravel?

Here is the usual form:

<form action="" method="POST">
   {{ csrf_field() }}
   <div class="uk-margin">
     <span class="uk-text-bold">Число:</span><input type="text" class="uk-input uk-form-width-small" name="bs">
   </div>
   <input type="hidden" value="{{ Auth::user()->name }}" name="user">
   <button id="put_bet" class="uk-button uk-button-primary" type="button">Поставить</button>
</form>

Here is the ajax itself:
<script type="text/javascript">
    $( document ).ready(function() {
        $("#put_bet").click(function(){
            $.ajax({
            url:'/ajax', //url в роуте
            type:'POST',
            data:{bs: 'cof_one'},
            success:function(data)
                {
                    $('.th-cof').html(data);
                },
            error: function(data)
                {
                    console.log('2');
                }
            });
        });
    });
    </script>

Here is the route:
Route::post('/ajax','[email protected]')->name('bets.store');

And here is the code from the controller:
public function store(Request $request)
    {
        $input = $request->all();
        $bet = new Bet();
        $bet->fill($input);
        if($bet->save()){
            Session::flash('store', 'Ваша ставка прошла!');
            return redirect()->route('home');
        }
    }

The essence of ajax is that the user enters a number in the field, clicks on the button, and the ajax code should work, which will change this number to the one seen by the user. If anything... I'm working with ajax+laravel for the first time, so don't judge too harshly.
Mistake
5a6e2584cce98863782270.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2018-01-28
@lemonlimelike

Where is your csrf token in your ajax request?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question