喵-见缝插针 2017-03-03 10:06 采纳率: 0%
浏览 18

Laravel用Ajax表单发布

I want to post a form with ajax. I get a errror in my console:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

I have a form:

{!! Form::open(array('url'=>'uren','method'=>'POST', 'id'=>'myform')) !!}
//inputs here etc..
{{ Form::button('Add', array('class' => 'btn btn-btn send-btn')) }}
{{ Form::close() }}

My Ajax:

$(document).ready(function(){
    $('.send-btn').click(function(){
        $.ajax({
            url: 'uren',
            type: "post",
            data: {
                'werk':             $('input[name="werk"]').val(),
                'starttijd':        $('input[name="starttijd"]').val(),
                'eindtijd':         $('input[name="eindtijd"]').val(),
                'omschrijving':     $('input[name="omschrijving"]').val(),
                '_token':           $('input[name="_token"]').val()},
            success: function(data){
                alert(data);
            }
        });
    });
});

In route(web.php):

Route::post('uren', 'User\UrenController@store');

In controller:

public function store() {

    if (\Request::ajax()) {

        if(Request::input('omschrijving') != '') {
            $omschrijving = Request::input('omschrijving');
        }else{
            $omschrijving = '';
        }

        DB::table('uren')->insert(
            [
                'userID' => Auth::user()->id,
                'datum' => Carbon\Carbon::now(),
                'projectID' => Request::input('werk'),
                'starttijd' => Request::input('starttijd'),
                'eindtijd' => Request::input('eindtijd'),
                'omschrijving' => $omschrijving
            ]
        );

    }

}

I think I need to do something with the csrf_token token. To pass the 500 error. But how can I do that?

[UPDATE] I added the token and I can send the token back and show the token in a alert. But when I add DB::table.... it still gives me the 500 error

[COMPLETE] The problem was my query.. a field was empty but it needs to be a string. I solved it.

  • 写回答

1条回答 默认 最新

  • weixin_33716557 2017-03-03 10:28
    关注

    If you dont want CSFR middleware you can disable that in laravel 5:

    To disable CSFR protection: go to this file:

       app/Http/Middleware/VerifyCsrfToken.php
    

    You can disable for all post request or for particular route. For more info xfer: https://laravel.com/docs/5.0/routing#csrf-protection

    OR

    just pass another parameter in your data "_token": "{{ csrf_token() }}",

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用