Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON not working with CodeIgniter and AJAX

Good day to all.

Im sorry if I asked this question which stopped me now on my development. I am just a newbie in jquery and json which dont know yet how to manipulate the data. Sorry for this.

As a background, here is my VIEW as I am using a CodeIgniter Framework. This Search button (in yellow) is not the default "Search" of the Boostrap Datatable. Since, as my design, I will search first the last name of the employee then hit the search button. If there are records to be retrieved, then I will add/display it in the datatable.

enter image description here

So here is my code:

VIEW:

<div class="panel-body">
    <div class="col-sm-3 col-md-3 pull-right">
        <form class="navbar-form" role="search">
            <div class="input-group">
                <input type="text" class="form-control" placeholder="Search" name="search_empname" id="search_empname" autocomplete="off">                                  

                <div class="input-group-btn">
                    <button class="btn btn-default" type="button" id="btnsearch">
                    <i class="glyphicon glyphicon-search"></i></button>                                     
                </div>                                  
            </div>
        </form>
    </div>

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover" id="employee_access_dataTables">
            <thead>
                <tr>
                    <th width="10%">Employee No.</th>
                    <th width="10%">Username</th>
                    <th width="10%">FirstName</th>
                    <th width="10%">MiddleName</th>
                    <th width="10%">LastName</th>                                       
                    <th width="10%">Prim. Role</th>
                    <th width="10%">Sec. Role</th>
                    <th width="10%">Email</th>
                    <th width="10%">Status</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>                         
            </tbody>
        </table>
    </div>

CONTROLLER

public function search_employee() {
    $search_empname = trim($this -> security -> xss_clean($this -> input -> get('lastname')));
    $data['empresult'] = $this -> msystems_admin -> search_employee($search_empname);
    echo json_encode($data);
}

AJAX

$('#btnsearch').click(function(){
    var gv_lname = $('#search_empname').val();

    if(gv_lname != ''){
        $.ajax({
            type:'GET',
            cache: false,
            datatype: 'json',
            url:'<?php echo site_url('csystems/search_employee');?>',
            data:'lastname='+gv_lname,
            success: function(data){
                console.log(data);  
                //I am not sure what to place here          
            }   
        });
    }               
});

Can you please emligthen me how to derive the output I desired? Thank you so much in advance.

NOTE: I cannot display the output of JSON even I do an ALERT. :(

UPDATE: Here's is the Network Tab of the Developer Tool of Chrome. enter image description here

And when I click the URL search_employee, I got nothing preview. enter image description here

enter image description here

UPDATE 2: If I changed my CONTROLLER something like this where I will not pass it like JSON.

public function search_employee() {
    $search_empname     = trim($this -> security -> xss_clean($this -> input -> get('lastname')));
    $data['empresult']  = $this -> msystems_admin -> search_employee($search_empname);
    //echo json_encode($data);

    foreach ($data['empresult'] as $row){
        $output  = $row -> fname;
        $output .= $row -> mname;
        $output .= $row -> lname;
    }

    echo $output;       

}

And in my AJAX, I remove the JSON as datatype such as this one below,

$('#btnsearch').click(function(){
    var gv_lname = $('#search_empname').val();

    if(gv_lname != ''){
        $.ajax({
            type:'GET',
            cache: false,
            //dataType: 'json',
            url:'<?php echo site_url('csystems/search_employee');?>',
            data:'lastname='+gv_lname,
            success: function(data){
                console.log(data);  
                //I am not sure what to place here                          
            }   
        });
    }               

});

Then I got a response as seen from the console logs. Please see the screenshot. enter image description here

I am not sure what's wrong with JSON... :(

like image 588
levi palmer Avatar asked Aug 09 '26 00:08

levi palmer


1 Answers

use

dataType: 'json',   instead of datatype: 'json',

T should be capital. Hope it works

like image 94
Punit Gajjar Avatar answered Aug 11 '26 14:08

Punit Gajjar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!