小编fig*_*ter的帖子

如何在laravel中通过ajax调用发送csrf令牌?

我有一个带有 method=post 的表单。在该表单中,我有一个 ajax 图像上传字段。当 ajax 调用过程时,验证发生 csrf 令牌不匹配错误。帮助我。这是我的代码..,

<script>
            $(document).ready(function(){
                $(document).on("click", "#upload", function() {
                    var file_data = $("#groupe_img").prop("files")[0];   // Getting the properties of file from file field
                    var form_data = new FormData();                  // Creating object of FormData class
                    form_data.append("file", file_data)              // Appending parameter named file with properties of file_field to form_data
                    form_data.append("csrftoken",document.mainform.csrftoken.value;)                 // Adding extra parameters to form_data
                    $.ajax({
                                url: "/upload_avatar",
                                dataType: 'script',
                                cache: false,
                                contentType: false,
                                processData: false,
                                data: form_data,                         // Setting the data attribute of ajax with …
Run Code Online (Sandbox Code Playgroud)

ajax csrf token mismatch laravel-5

5
推荐指数
1
解决办法
9392
查看次数

标签 统计

ajax ×1

csrf ×1

laravel-5 ×1

mismatch ×1

token ×1