如何使用Codegniter从Ajax Jquery中的控制器获取多个参数?

时间:2016-04-29 12:52:12

标签: javascript jquery ajax codeigniter

我从特定ID的文件夹中获取图像列表。现在我正在获取文件名,但我也希望获得上传路径。

如何在一个函数中获取两个数据。

Jquery代码:

listFilesOnServer(project_id);

    function listFilesOnServer (project_id) {
        var items = [];
        uploadURI = uploadURI+'/'+project_id;
        console.log(project_id+'--KAL--'+uploadURI);
        $.getJSON(uploadURI ,function(data,path) {
            console.log(data);
            $('div #list-group').html("").html(items.join(""));
        });
    }

控制器代码:

function listFiles() {
        $this->load->helper('file');
        $project_id = $this->uri->segment(3);
        $builders_id = $this->admin_model->getBuilderID($project_id);
        $UPLD_PATH = $this->admin_model->builder_UPLD_PATH($builders_id);
        $upload_path = "./application/assets/images/" . $UPLD_PATH;
        $files = get_filenames($upload_path);
        echo json_encode($files);
    }

1 个答案:

答案 0 :(得分:0)

您应该修改控制器操作,使其返回json_encode(array('files'=>$yourFiles, 'filePath'=>$yourFilePath) );,如下所示:

function listFiles() {
        $this->load->helper('file');
        $project_id = $this->uri->segment(3);
        $builders_id = $this->admin_model->getBuilderID($project_id);
        $UPLD_PATH = $this->admin_model->builder_UPLD_PATH($builders_id);
        $upload_path = "./application/assets/images/" . $UPLD_PATH;
        $files = get_filenames($upload_path);
        echo json_encode(array('files'=>$files, 'uploadPath'=>$upload_path) );
        exit();
}

然后修改你的jquery代码来处理json响应并提取响应,如下所示:

listFilesOnServer(project_id);

function listFilesOnServer (project_id) {
    var items = [];
    uploadURI = uploadURI+'/'+project_id;
    console.log(project_id+'--KAL--'+uploadURI);
    $.getJSON(uploadURI ,function(data,path) {
        //Your upload path
        console.info("UPLOAD PATH: "+data.uploadPath);
        //Your files
        console.log(data.files);
        //Your processing logic goes here
        $('div #list-group').html("").html(items.join(""));
    });
}