简体   繁体   中英

Laravel Ajax posting data into the database

This is my index.php

    <div class="container">
    <h1>Drink A Coffee today</h1> <hr>
       <h2>Coffee Orders</h2>
    <ul id="orders">
    <p><li class="highlight"></li></p>
    <p><li class="highlight"></li></p>
    </ul>

    <h4>Add a Coffee Order</h4>

    {!! Form::open(['url' => 'index', 'method' => 'POST']) !!}
     <div class="form-group" role="form">
    <p>
        <label for="name">Name:</label>
        <input id="name" type="text" name="name" class="form-control">
    </p>

    <p>
        <label for="drink">Drink:</label>
        <input id="drink" type="text" name="drink" class="form-control">
    </p>
    <p>
        <button id="add-order" class="btn btn-default" type="submit">Add!</button>
    </p>
</div>
{!! Form::close() !!}
</div>

Below is my controller

public function makeOrder() {
      $post = Request::all();

      if (Request::ajax()) {
        $order = new Order;

        $order->name = $post['name'];
        $order->drink = $post['drink'];

        $order->save();
        $response = array(
        'status' => 'success',
        'msg' => 'Option created successfully',
        );

        return Response::json( $response );
      }
    }

//Below is my Route

Route::post('index', 'MakeOrderController@makeOrder');

then my Ajax script

$(document).ready(function() {

    $('#add-order').click(function(e) {
        e. preventDefault();
        //setting variables based on the input fields
        var inputName = $('input[name="name"]').val();
        var inputDrink = $('input[name="drink"]').val();
        var token = $('input[name="_token"]').val();
        var data = {name:inputName, drink:inputDrink, token:token};

        var request = $.ajax({

            url: "index",
            type: "POST",
            data: data,
            dataType:"html"
            });

            request.done(function( msg ) {
                var response = JSON.parse(msg);
                console.log(response.msg);
            });

            request.fail(function( jqXHR, textStatus ) {
                console.log( "Request failed: " + textStatus );
            });
        });



});

But any time i try to submit, nothing happends and when i inspect element, i get this error "Failed to load resource: the server responded with a status of 500 (Internal Server Error) Request failed: error"

Ah, I think I see it now. In your AJAX data, you are sending 'token', but Laravel is expecting this parameter to be named '_token'.

You can make it easier on yourself by just using jQuery's serialize() function on the form, instead of retrieving each field as a variable. Ex:

var data = $(this).closest('form').serialize();

Instead of var inputName, var inputDrink, etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM