zoukankan      html  css  js  c++  java
  • Laravel 419错误 -ajax请求 错误解决办法(CSRF验证)

    两种解决办法。选择适合自己的。

    第一种解决方法

    适用于可以把js写在不被laravel框架渲染的js文件中的操作

    1.在页面上添加 

     <meta name="csrf-token" content="{{ csrf_token() }}"》

    2.然后在页面的script标签中添加

    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});

    第二种解决方法

    适用于改部分js可以实际被laravel框架解析的操作

    $.ajaxSetup({headers: {'X-CSRF-TOKEN'"{{ csrf_token() }}"}});

    3.如果你提交的页面是html的form页面的话,

    你只需要在你的form表单里面填写上下面的一段就行啦

    <form method="POST" action="/profile">
        {{ csrf_field() }}
        ...
    </form>

    4.如果你是在页面里面设置了ajax的请求

    你只需要在页面的ajax请求里面设置_token即可,

    // 封装提交的记录的函数
        function sendLog(type){
            $.ajax({
                type: 'POST',
                url: '/log',
                data: {'share_type': type, 'url_info': shareLink, 'invitation_code''{{$invitation_code}}''_token':'{{csrf_token()}}'},
                dataType: 'json',
                success: function($rtn){
                    console.log($rtn);
                }
            });
        }

     

    两种解决办法。选择适合自己的。

    第一种解决方法

    适用于可以把js写在不被laravel框架渲染的js文件中的操作

    1.在页面上添加 

    1
     <meta name="csrf-token" content="{{ csrf_token() }}"》

    2.然后在页面的script标签中添加 

    1
    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});

    第二种解决方法

    适用于改部分js可以实际被laravel框架解析的操作

    1
    $.ajaxSetup({headers: {'X-CSRF-TOKEN'"{{ csrf_token() }}"}});

    3.如果你提交的页面是html的form页面的话,

    你只需要在你的form表单里面填写上下面的一段就行啦

    1
    2
    3
    4
    <form method="POST" action="/profile">
        @csrf
        ...
    </form>

    或者

    1
    2
    3
    4
    <form method="POST" action="/profile">
        {{ csrf_field() }}
        ...
    </form>

    4.如果你是在页面里面设置了ajax的请求

    你只需要在页面的ajax请求里面设置_token即可,

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    // 封装提交的记录的函数
        function sendLog(type){
            $.ajax({
                type: 'POST',
                url: '/log',
                data: {'share_type': type, 'url_info': shareLink, 'invitation_code''{{$invitation_code}}''_token':'{{csrf_token()}}'},
                dataType: 'json',
                success: function($rtn){
                    console.log($rtn);
                }
            });
        }
  • 相关阅读:
    数据结构之 内部排序---交叉排序(没啥特别的!!!)
    数据结构之 图论---基于邻接矩阵的广度优先搜索遍历(输出bfs遍历序列)
    数据结构之 图论---图的深度遍历( 输出dfs的先后遍历序列 )
    数据结构之 排序---折半插入排序(时间复杂度 O(nlog2 n) )
    HDU 1022 之 Train Problem I
    Bestcoder round 18---A题(素数筛+素数打表+找三个素数其和==n)
    Bestcoder round 18----B题(一元三次方程确定区间的最大值(包含极值比较))
    操作字典
    在线压缩图片
    JSON转C#实体类
  • 原文地址:https://www.cnblogs.com/seanpan/p/11591903.html
Copyright © 2011-2022 走看看