drag2458 2015-07-31 00:40
浏览 64
已采纳

PHP的CodeIgniter访问控制器要求

I am using codeigniter for my project and I am stuck trying to figure this out.

I have some javascript that needs to perform an AJAX call to fetch some results based on a dropdown value that was selected.

function fetchLines(){
 $.ajax({
    url: baseURL + "resources/ajax.php?node=fetchLines",
    type: 'GET',
    cache: false,
    data: {
       lineType: 'business'
    },
    error: function(err) {
        alert(err.statusText);
    },
    success: function(data) {

        console.log(data);

    }

});
}

In this AJAX file, I am trying to include my controller and then access the function within it.

<?php
define('BASEPATH', "AJAX");
require_once('../application/controllers/Project.php');


switch($_REQUEST['node']){

    case 'fetchLines':
        $objLines = new Project();
        $objLines->fetchLines($_REQUEST['lineType']);
    break;

}

?>

My CI Controller then has a private function in it which I am trying to call to get the data I need:

private function fetchLines($lineType){
    $lines = $this->project_model->fetchLines($lineType);
    return $lines;
}

My goal here is to have an AJAX file or controller (if needed) be used for all my AJAX calls. It needs to be able to access a controller and return data.

With the current code above, I am getting the error: Class 'CI_Controller' not found in <b>C:\xampp\htdocs\blueprint\application\controllers \Project.php

Is there a better way to handle situations like this? I'm not an expert with OOP but some reading suggested something along these lines.

  • 写回答

3条回答 默认 最新

  • dongzhuo0895 2015-07-31 05:43
    关注

    why you are not sending this request to the controller method instead ?

            function fetchLines(){
             $.ajax({
                url: baseURL + "controller-name/method-name",
                type: 'GET',
                cache: false,
                data: {lineType: 'business'},
                error: function(err) {
                    alert(err.statusText);
                },
                success: function(data) {
    
                    console.log(data);
    
                }
    
            });
            }
    

    NOTE and in controller you can access these values as

    function method-name(){
        echo $this->input->get('lineType');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?