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 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多