为什么我的.htaccess在ajax发布请求中将我重定向到主页?

时间:2019-03-09 15:56:48

标签: ajax .htaccess codeigniter post csrf-protection

Hello Stackoverflow社区,

我当时在从事代码点火器的开发,我认为目前需要在视图和控制器之间进行通信。我认为最好的方法是执行ajax发布请求。那就是我的403 http状态。我在线搜索了解决方案,并在stackoverflow ask中以6票获得了@Tipihack响应。该请求的状态为200。

POST带有以下网址“ http://localhost/project/controller/method”,并通过检查代码->网络

常规

Request URL: http://localhost/calendar/basecontroller/getagendadate
Request Method: POST
Status Code: 200 OK
Remote Address: [::1]:80
Referrer Policy: no-referrer-when-downgrade

请求标头

Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7
Connection: keep-alive
Content-Length: 54
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: csrf_cookie_name=80c14ce169a5f8110be579a59693e058
Host: localhost
Origin: http://localhost
Referer: http://localhost/calendar/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
X-Requested-With: XMLHttpRequest

响应标题

Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
Date: Sat, 09 Mar 2019 15:13:34 GMT
Keep-Alive: timeout=5, max=98
Server: Apache/2.4.38 (Win64) OpenSSL/1.0.2q PHP/5.6.40
Set-Cookie: csrf_cookie_name=c3d91845df538fa001c64267f440fc32; expires=Sat, 09-Mar-2019 17:13:34 GMT; Max-Age=7200; path=/
Transfer-Encoding: chunked
X-Powered-By: PHP/5.6.40

[控制器]

public function getAgendaDate(){
    $sql = 'select * from agenda';
    $result = $this->db->query($sql);
    $send = array('token' => $this->security->get_csrf_hash());

    if (!headers_sent()) {
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: ' . date('r'));
        header('Content-type: application/json');
    }
    exit(json_encode($send, JSON_FORCE_OBJECT));
}

[查看]

$(document).ready(function() {
        //initialize CFG
        CFG = {
            url: '<?php echo $this->config->item('base_url');?>',
            token: '<?php echo $this->security->get_csrf_hash();?>',
            name: '<?php echo $this->security->get_csrf_token_name();?>'
        };

        // setup token
        $.ajaxSetup({data: {token: CFG.token}});
        $(document).ajaxSuccess(function(e,x) {
            var result = $.parseJSON(x.responseText);
            $.ajaxSetup({data: {token: result.token}});
        });
    }).
    // this request run when dispacher an onclick event
    $.ajax({
        url: "basecontroller/getagendadate",
        type: "POST", //send it through get method
        data: { 
            date: '02-02-2019'
        },
        success: function(response) {
            console.log(response);
        },
        error: function(xhr) {
            //Do Something to handle error
            console.log(xhr)
        }
    });

[问题]-问题是,响应请求,我正在从索引页接收整个代码。因此,我认为问题必须出在.htaccess上。

[。htaccess] 所有代码

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L]

为什么我什么时候请求控制器方法?我可以重定向到我的主页吗?

0 个答案:

没有答案