无法在Laravel中使用模式和Ajax插入表

时间:2019-09-13 15:33:35

标签: javascript php ajax laravel

我无法使用引导模态和一些Ajax将a插入数据库。有人能帮我吗。我收到错误INSERT INTO mytable SET id = ?, col23 = NULL; 。我一直在寻找与此类似的问题,但我找不到它。对不起,我是AJAX的新手。有谁知道如何解决这一问题。 TIA

模式

Uncaught SyntaxError: Invalid shorthand property initializer

Ajax

          <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                  <div class="modal-dialog modal-dialog-centered" role="document">
                    <div class="modal-content">
                      <div class="modal-header"> 
                        <h5 class="modal-title" id="exampleModalCenterTitle">Supplier</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                      <p style="font-weight: bold;">Name </p>
                        <input  style="text-transform:uppercase"  type="text" class="form-control" id="supplier"/>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary" id="add_supplier">Add</button>
                      </div>
                    </div>
                  </div>
                </div> 

控制器


            <script type="text/javascript">

            $(document).ready(function(){

                $('#add_supplier').click(function(e){
                    e.preventDefault();
                    var input = $('#supplier').val();
                    $.ajaxSetup({
                        headers:{
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        }
                    });

                    $.ajax({
                        url: "{{    url('/supplier')    }}",
                        method: 'post',
                        data: {
                            name = input
                        },
                        success: function (res){
                            console.log(res);
                            window.location.href = '{{route("supplier.index")}}';
                        }
                    });
                });

            });



            </script>

路线

    public function store(Request $request)
    {
        $data = $request->all();

        $data['name'] = ($data['name']);

        Supplier::create($data);

        return response()->json($data);
    }

1 个答案:

答案 0 :(得分:1)

您的数据必须作为javascript对象发送,因此正确的语法应为

data: {name: input}
相关问题