如何在Codeigniter中从视图调用模型函数?

时间:2019-03-09 18:16:15

标签: codeigniter model-view-controller

在我看来,我有一个输入元素,它接收一个日期,一旦接收到该日期,它就会发出一个事件,在该事件中,我想执行该模型的功能,该功能基于接收的日期在数据库中进行查询。该怎么做?

[更新] 09-03-19 -从视图ajax请求和控制器校准模型调用控制器

[问题] -请求的响应是我的home.php(我的看法)

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 个答案:

没有答案