Codeigniter form_open_multipart显示403 Forbidden(CSRF)

时间:2018-06-01 19:45:58

标签: codeigniter csrf http-status-code-403 ajaxform ajax-forms

我试图通过ajax帖子发布数据,但不知何故无法发布。我被禁止了403。幸运的是,我的其他ajax帖子正在正确提交,因为它们正在重新生成令牌。但是在form_open_multipart中,它无效。

CI配置

$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'my_token';
$config['csrf_cookie_name'] = 'my_cookie';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();

form_open运行良好,但在多部分的情况下它运行不正常!!!!

我的HTML

<?php $parameter = array('id' => 'frm_id', 'class' => 'form-horizontal'); echo form_open_multipart('controller/save',$parameter) ;?>
<div class="form-body">
    <div class="form-group">
        <label class="col-md-3 control-label">Name</label>
        <div class="col-md-6">
            <div class="input-icon">
                <i class="fa fa-bell-o"></i>
                <input type="text" id="catId" class="form-control " placeholder="Type Something..." name='name' /> 
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">Description</label>
        <div class="col-md-6">
            <div>
                <textarea class="form-control" rows="3" name="description"></textarea>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">Image 1</label>
        <div class="col-md-6">
            <div>
                <input type="file" name="thumb_image">
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">Image 2</label>
        <div class="col-md-6">
            <div>
                <input type="file" name="banner_image">
            </div>
        </div>
    </div>
</div>
<div class="form-actions">
    <div class="row">
        <div class="col-md-offset-3 col-md-9">
            <button type="submit" class="btn btn-flat green">Submit</button>
            <button type="button" class="btn btn-flat grey-salsa">Cancel</button>
        </div>
    </div>
</div>
<?php echo form_close() ;?> 

我的JS

<script type="text/javascript">
    var frmSave = $('#frm_id'); 
    frmSave.on('submit', function(event){
        event.preventDefault();
        $form=$(this);
        var fd = new FormData($('#frm_id')[0]);
        $.ajax({
            url: $form.attr('action'),
            type: $form.attr('method'),
            dataType: 'json',
            data: fd,
            contentType: false,
            cache: false,  
            processData: false,
            success: function(data){
                console.log(data)
            }
        })             
    });
</script>

我仍然收到此消息

  

遇到错误。您请求的操作不是   允许的。

0 个答案:

没有答案