【问题标题】:Laravel Forbidden Access when using Ajax使用 Ajax 时 Laravel 禁止访问
【发布时间】:2020-06-16 04:58:29
【问题描述】:

我安装了一个带有身份验证的全新 Laravel 应用程序。我正在使用拉拉贡。登录、注册、重置密码页面工作正常。我为用户创建了一个配置文件控制器来编辑配置文件。但是,当通过 Ajax 提交表单时,它给了我一个Forbidden - You don't have permission to access / / /profile/edit_profile on this server.

         class ProfileController extends Controller
            {
                //

                function index()
                {
                    return view('profile.index');
                }

                function edit_profile(Request $request)
                {
                    $txt_midname = $request->input('txt_midname');
                    $txt_firstname = $request->input('txt_firstname');
                    $txt_lastname = $request->input('txt_lastname');
                    $extname = $request->input('extname');
                    $user = Auth::user();
                    $user->firstname = $txt_firstname;
                    $user->midname = $txt_midname;
                    $user->lastname = $txt_lastname;
                    if ($user->save()) {
                        return 'ok';
                    }
                }

            }

这里也是路线:

Route::post('/profile/edit_profile', 'ProfileController@edit_profile')->name('edit_profile');

和视图:

            $('#btn_update').on('click', function() {
                var btn_text = $('#btn_update').text();
                var txt_firstname = $.trim($('#txt_firstname').val());
                var txt_midname = $.trim($('#txt_midname').val());
                var txt_lastname = $.trim($('#txt_lastname').val());
                var extname = $.trim($('#extname').val());

                $.post(baseUrl + '/profile/edit_profile', {
                    '_token': token,
                    'txt_midname': txt_midname,
                    'txt_firstname': txt_firstname,
                    'txt_lastname': txt_lastname,
                    'extname': extname
                }, function(data) {
                    if (data == 'ok') {
                        window.location.reload();
                    }
                })
            })

【问题讨论】:

    标签: laravel laragon


    【解决方案1】:

    您需要将 csrf 令牌注入到您的请求中。

    $.post({
      headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
      },
      url: '/admin/gallery/create/ajax',
      data: {}, 
      method: 'POST',
      success: function(response) {
      },
      error: function(error) {
      }
    })
    

    或者如果您希望每个 ajax 请求都注入 csrf 令牌,您也可以这样做。

    $.ajaxSetup({
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
      }
    });
    

    【讨论】:

      【解决方案2】:

      这是我的错。问题出在我的 javascript 中的 baseUrl 上。应该是var baseUrl = "{{url('')}}";而不是var baseUrl = '{{url('')}}';

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-16
        • 1970-01-01
        • 1970-01-01
        • 2015-02-25
        • 1970-01-01
        • 1970-01-01
        • 2019-03-15
        • 2019-02-24
        相关资源
        最近更新 更多