提问者:小点点

禁止Codeigniter Ajax


我对这个被禁止的问题感到困惑。首先,我查看了相关的stackoverflow帖子,并在谷歌上搜索了足够多,但仍然不知道。

项目详细信息:

- Currently used libraries:
    1. Carabiner (https://github.com/bcit-ci/CodeIgniter/wiki)
    2. Template (https://github.com/jenssegers/codeigniter-template-library)
    3. hmvc
    3. instagram_api 

- CSRF Protection turned on (I don't want false the protection)

- Followed main posts
    1. https://stackoverflow.com/questions/40225908/ajax-post-not-working-codeigniter
    2. https://stackoverflow.com/questions/22527412/403-forbidden-access-to-codeigniter-controller-from-ajax-request

- ISSUE: 403 Forbidden ("The action you have requested is not allowed.")

HTML

Instagram.php

form_open()函数为访问令牌生成隐藏字段,并将其包含在ajax发布数据中。

<input type="hidden" name="csrf_token_localhost" value="3f5887fd41ac4eaa9b558afa7cb4a6de">
...
<?php echo form_open('admin/getInstagramAccessToken', array('id' => 'instagram_settings', 'class' => 'form-horizontal row-border')); ?>
...
    <div class="col-sm-8 col-sm-offset-2">
        <?php  echo form_submit('submit', 'Get my access token', ['class' => 'btn-primary btn btn-get-token']); ?>
    </div>
...
<?php echo form_close(); ?>

JavaScript

AdminScript.js

$('#instagram_settings').submit(function( event ) {
    var posting_data = $( this ).serializeArray();
    event.preventDefault();
    
    $.ajax({
        type: 'POST',
        dataType: 'json',
        async: true,
        cache: false,
        data: posting_data,
        url: 'admin/getInstagramAccessToken',
        success: function(json) {

            try{
                console.log(json);
            }catch(e) {
                console.log('Exception while request..');
            }

        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(jqXHR, textStatus, errorThrown);
        },
        complete: function() {
            console.log('ajax complete');
        }
    })

控制器

admin.php

public function getInstagramAccessToken() 
{
    if ($this->input->is_ajax_request()) 
    {
        $response['status'] = 'success';
        echo json_encode($response);
    }
    else
    {
        // if the request if not ajax then show 404 error page
        show_404();
    }
    
}

当我使csrf保护状态为假时,它们都能正常工作。或者当我将post类型从“post”改为“get”时。但我想保持状态为真,并使用“post”方法。

两个图像的Dropbox链接:

>

  • 表单加载后隐藏csrf令牌字段

    过帐数据

    https://www.dropbox.com/sh/e93ubgwzv9zir5j/aaa6vf5iwc1m7rtpgwgcpub4a?dl=0


  • 共2个答案

    匿名用户

    您可以通过发送带有AJAX请求的CSRF令牌来传递这个问题。这是因为您已将CSRF保护置于“开”模式。只有通过GET发送请求或显式地将CSRF'OFF'关闭时,才能绕过这一点,当然,这是不建议的。

    因此,解决方案是在AJAX请求中包含CSRF令牌。

    阅读这篇文章,了解更多关于在AJAX请求上添加CSRF令牌的信息。

    编辑:理想情况下,您应该通过ajax请求中的数据选项发送CSRF令牌。查看此链接以获取更多信息

    匿名用户

    请在每一个有POST类型的表单中添加此片段。

                 <?php  $csrf = array(
                'name' => $this->security->get_csrf_token_name(),
                'hash' => $this->security->get_csrf_hash()
                 ); ?>   
                  <input type="hidden" name="<?php echo $csrf['name']; ?>" 
                  value="<?php echo$csrf['hash']; ?>" />