如何从laravel post方法中的ajax中删除403错误

时间:2018-09-23 11:53:28

标签: ajax laravel server routes http-status-code-403

我的代码的问题是,如果我尝试通过ajax发送数据,则会给我错误,但是我的其他帖子形式都可以正常工作。因此,我确定在route,route group或csrf feild中必定存在错误。将csrf字段设置为表格,在删除按钮中,在输入标签中隐藏,在标题中但修女工作。代码在我的计算机上运行正常,但在服务器上却显示403 Forbidden。这已经是4天了。请提供您宝贵的见解和建议。

       <!DOCTYPE html>
        <html>
        <head>
        <meta name="csrf-token" content="{{ csrf_token() }}" />
        <title></title>
        <style type="text/css" media="screen">


        </style>
        <script src="{{asset('js/admin_update_ajax.js')}}"></script>
        <link href="{!! asset('css/table_layout.css') !!}" media="all" rel="stylesheet" type="text/css" />
        </head>
        <body>

        <table class="table">
        <thead>
        <tr>
        <th style="text-align:center">Id</th>
        <th style="text-align:center">Extra Details</th>
        <th style="text-align:center">Update</th>
        <th style="text-align:center">Delete</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($details as $value): ?>
        <tr>
        <td style="font-weight: bold">{{ $value->id }}</td>

        <td>{{ $value->extra_detail }}</td>
        <td> <button class="btn btn-success btn-md" data-toggle="modal" data-target="#modalForm">Update</button></td>
        <td><button type="button" class="btn btn-danger">Delete</button></td>
        </tr>
        </tr>
        <?php endforeach ?>
        </tbody>
        </table>


        <div class="modal fade" id="modalForm" role="dialog">
        <div class="modal-dialog">
        <div class="modal-content">
        <!-- Modal Header -->
        <div class="modal-header">
        <h4 class="modal-title" id="myModalLabel">Contact Form</h4>
        <button type="button" class="close" data-dismiss="modal">
        <span aria-hidden="true">&times;</span>
        <span class="sr-only">Close</span>
        </button>

        </div>

        <!-- Modal Body -->
        <div id="showmessage">
        <div class="modal-body">
        <p class="statusMsg"></p>
        <div id="hidebody">
        <form role="form" id="update_form" method="POST">
        <input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">

        <div >Id:<span id="idfromtable"></span></div>
        <div class="form-group">
        <label for="starting_date">Starting Date</label>
        <input type="date" class="form-control" id="starting_date" placeholder="Enter starting date">
        </div>
        <div class="form-group">
        <label for="ending_date">Ending Date</label>
        <input type="date" class="form-control" id="ending_date" placeholder="Enter Ending date">
        </div>
        <div class="form-group">
        <label for="extra_details">Extra Details</label>
        <textarea  class="form-control" id="extra_detail" placeholder="Enter Ending date"></textarea>
        </div>
        </form>
        </div>
        </div>
        </div>
        <!-- Modal Footer -->
        <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary submitBtn" onclick="submit_update_form()">SUBMIT</button>
        </div>
        </div>
        </div>
        </div>
        </body

        > Blockquote

Java脚本代码为

function submit_update_form(){

        var starting_date = document.forms["update_form"]["starting_date"].value;
        alert(starting_date);
        var ending_date = document.forms["update_form"]["ending_date"].value;
        alert(ending_date);
        var extra_detail = document.forms["update_form"]["extra_detail"].value;
        alert(extra_detail);



        $.ajax({
        type: "POST",
        headers: {
            'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
        },
        url: "/check",
        data: {
        id:id,
        table_name:table_name,
        exam_name:exam_name,
        link:link,
        extra_detail:extra_detail

        },
        success: function(resultData) { alert(resultData) }
        });

        alert("update end from modal");
        }

这是我的路线文件

Route::post('/check', 'AdminController@index');

我的控制器

public function index()
{

    return "Data Inserted Successfully In Table ";
}

2 个答案:

答案 0 :(得分:0)

  

您必须在请求标头中传递.../wildfly/standalone/deployments/

csrf-token

答案 1 :(得分:0)

添加以下代码

<script>
    $(function () {
        $.ajaxSetup({
            headers: {
                'X-CSRF-Token': "<?php echo csrf_token() ?>"
            }
        });
    });
</script>