(匿名函数)使用AJAX传递数据时

时间:2016-08-13 04:25:24

标签: php ajax laravel-5.2

我是LARVEL的新用户,当我遇到这个问题时,希望有人可以帮助我

我用来开发的东西=> PHP Laravel 5.2

我遇到的错误=> POST 500内部服务器错误指向=> (匿名函数)

这是我的ajaxcreate.blade.php

@extends('layouts.app')

@section('content')
<div class="col-md-10 col-md-offset-1">
    <div class="col-md-8">
        <input type="text" class="form-control" id="name" name="name"
            placeholder="Enter some name" required>
        <p class="error text-center alert alert-danger hidden"></p>
    </div>
    <div class="col-md-4">
        <button class="btn btn-primary" type="submit" id="add">
            <span class="glyphicon glyphicon-plus"></span> ADD
        </button>
    </div>
</div>

<div class="col-md-10 col-md-offset-1">
    <table class="table table-borderless" id="table">
        <thead>
            <tr>
                <th class="text-center">ID</th>
                <th class="text-center">Name</th>
                <th class="text-center">Details</th>
                <th class="text-center">Actions</th>
            </tr>
        </thead>
        @foreach($data as $item)
        <tr class="item{{$item->id}}">
            <td>{{$item->id}}</td>
            <td>{{$item->name}}</td>
            <td>{{$item->details}}</td>
            <td><button class="edit-modal btn btn-info" data-id="{{$item->id}}" data-name="{{$item->name}}">
                    <span class="glyphicon glyphicon-edit"></span> Edit
                </button>
                <button class="delete-modal btn btn-danger" data-id="{{$item->id}}" data-name="{{$item->name}}">
                    <span class="glyphicon glyphicon-trash"></span> Delete
                </button>
            </td>
        </tr>
        @endforeach
    </table>
</div>


<script>
$("#add").click(function() {

    $.ajax({    // this is where my console said the anonymous function
        type: 'post',
        url: 'addItem',
        dataType: 'json',
        data: {
            '_token': $('input[name=_token]').val(),
            'name': $('input[name=name]').val()
        },
        success: function(data) {
            if ((data.errors)) {
                $('.error').removeClass('hidden');
                $('.error').text(data.errors.name);
            } else {
                $('.error').remove();
                $('#table').append("<tr class='item" + data.id + "'><td>" + data.id + "</td><td>" + data.name + "</td><td><button class='edit-modal btn btn-info' data-id='" + data.id + "' data-name='" + data.name + "'><span class='glyphicon glyphicon-edit'></span> Edit</button> <button class='delete-modal btn btn-danger' data-id='" + data.id + "' data-name='" + data.name + "'><span class='glyphicon glyphicon-trash'></span> Delete</button></td></tr>");
            }
        },
    });
    $('#name').val('');
});
</script>
@endsection

这是我的AjaxController.php

public function addItem(Request $request) 
    {

        $data = new Product ();
        $data->name = $request->name;
        $data->details = $request->name;
        $data->save ();
    }

这是我的routes.php

Route::post('/addItem','AjaxController@addItem');

Route::get('/readItem','AjaxController@readItems');

1 个答案:

答案 0 :(得分:0)

问题是不匹配令牌

所以我所要做的就是改变

data: {
            '_token': $('input[name=_token]').val(),
            'name': $('input[name=name]').val()
      },

data: {
            '_token': '{{ csrf_token() }}',
            'name': $('input[name=name]').val()
      }