Laravel 4 CSRF 表单通过 Ajax 调用提交 [英] Laravel 4 CSRF form submit through Ajax call

查看:24
本文介绍了Laravel 4 CSRF 表单通过 Ajax 调用提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Stackoverflow 和 Laravel 4 的新手,我正在尝试通过 jquery ajax 提交表单.我 serializeArray() 表单并使用 json 提交它.在 laravel 中,在我检查提交的 csrf 令牌的路由中,转到我的控制器以获取流程输入.当 _token 输入与 serializeArray() 一样位于数组中时,csrf 检查不起作用.我必须获取 _token 值并提交作为单独的 ajax 数据.我想知道是否有办法告诉 csrf 过滤器在哪里运行以检查输入数组中的 hte 字段 _token 并使用它?这是我的代码:

I'm new to both Stackoverflow and Laravel 4, I am trying to submit a form through jquery ajax. I serializeArray() the form and use json to submit it. In laravel, in the routes I check the csrf token submitted, the go to my controller for process inputs. The csrf check doesn't work when the _token input is in an array as with serializeArray(). I have to get the _token value and submit is as a seperate ajax data. I'm wondering if there is a way to tell csrf filter where ever its running to check the input array for hte field _token and use that? here is my code:

routes.php

Route::post('searchAjax', array('before' => 'csrf', 
                                'uses' => 'SearchController@searchAjax'));

这是我不希望它读取数组 form[0]['_token] 但我不知道如何告诉它这样做的地方.

This is where I wan't it to read the array form[0]['_token] but I don't know how to tell it to do that.

index.js
    var form = $('#search').serializeArray();
var token = $('#search > input[name="_token"]').val();
$.ajax({
    type: 'post',
    url: 'searchAjax',
    dataType: 'json',
    data: { _token: token, form: form },
    success: function(data) {
        for(var key in data)
        {
            //alert(key)
        }
        //alert(data.message);
    }
});

我想摆脱 ajax 调用中的 { _token: token, form: form } 并且只拥有一个包含 _token 的数组的form".这是html:

I want to get rid of { _token: token, form: form } in the ajax call and just have 'form' that is an array with _token in it already. here is the html:

<form id="search" class="form-horizontal" accept-charset="UTF-8" action="http://testing:998/search" method="POST">
  <input type="hidden" value="6GBbv9LmnOdL8UcOsm8DDJ4Bfi6eGcQRlC9SPdL4" name="_token">
<div class="control-group">
  <label class="control-label" for="title">Book Titles</label>
  <div class="controls">
    <input id="title" class="input" type="text" value="click to search book titles" name="title">
  </div>
</div>
<div class="control-group">
<div class="control-group">
<div class="control-group">
</form>

先谢谢你.:)

推荐答案

另一种选择是使用 Kelt Docins 概述的方法,即使用 jquery 在标头中发送令牌,例如.在您的 jquery/javascript 引导添加的某处:

Another option is to use the approach outlined by Kelt Dockins which is to send the token in the headers using jquery, eg. somewhere in your jquery/javascript bootstrapping add:

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

这将从标头元数据标记中获取 CSRF 令牌,并将其包含在每个 ajax 请求的标头中.然后你需要将元数据和令牌添加到你的 Laravel 模板中,例如.

This will get the CSRF token from a header meta data tag and include it in the headers of each ajax request. Then you need to add the metadata and token into your Laravel templates, eg.

<head>
    <title>My Page</title>
    <meta name="csrf-token" content="<?= csrf_token() ?>">

您还需要修改 CSRF 过滤器以检查 ajax 请求标头以及默认 Input::get('_token')

You also need to modify the CSRF filter to check the ajax request header as well the default Input::get('_token')

Route::filter('csrf', function()

{
   $token = Request::ajax() ? Request::header('X-CSRF-Token') : Input::get('_token');
   if (Session::token() != $token) {
      throw new IlluminateSessionTokenMismatchException;
   }
});

这篇关于Laravel 4 CSRF 表单通过 Ajax 调用提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆