Retrieve and display json data from codeigniter function

How can to display data from database (SQL) using ajax (json) and codeigniter or php? i’ve tried to do it but the result is “undefined” here is my code:

jquery codes:

$('[id^="menuitem_"]').on('click',function(){
var menuID = $(this).attr('id').split("_")[1];
        $.ajax({
            url:"<?php echo base_url();?>Vendors_search/Get_Business_By_Type",
            method:"POST",
            dataType: "text",
            data: {menuID:menuID},
            success:function(resp){

              var obj = $.parseJSON(resp);
              var values = Object.values(obj); 
              $.each(obj,function(key,val){

              $('.business_id').append('<strong>' + val.business_name + '</strong>');
            });
    }

        });

Codeigniter codes:

function Get_Business_By_Type(){
    $obj = $this->input->post('menuID');     
    if($this->Search_obj_model->getBusinessType($obj)){
    $bus_type = $this->Search_obj_model->getBusinessType($obj);  
    $result['business_details'] = $this->Search_obj_model->getBusinessType($obj);
        }
        else{
            $result['message'] = 'Oooops! We could not find your request. Try again later';
        }
    $this->output->set_content_type('application/json');
    $this->output->set_output(json_encode($result));
    $string = $this->output->get_output();
    echo $string;
    exit();
 }

#php #json #ajax #codeigniter

5 Likes181.90 GEEK