简体   繁体   中英

How to get multiple parameters from controller in Ajax Jquery using Codeigniter?

I am getting list of images from a folder for particular ID. Right now I am getting file names but I also want to get upload path.

How to get both data in one function.

Jquery Code:

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(""));
        });
    }

Controller Code:

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);
    }

You should modify your controller action so that it returns an json_encode(array('files'=>$yourFiles, 'filePath'=>$yourFilePath) ); like below :

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();
}

Then modify your jquery code to handle the json response and extract the response like below :

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(""));
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM