0

I'm new to CodeIgniter and still learning. I'm trying to make a Ajax call to one of my controllers, which will return data from a module and then echo the data back to Ajax wrapped in json_encode(). When I echo the data in my controller it outputs valid JSON, but when I use console.log() in my Ajax function the result is always null.

This is my Javascript:

$( document ).ready(function() {
    get_listings();
});

function get_listings(province = false, city = false, category = false, slug = false, order_by = false, 
                      limit = false, offset = false){

    $.ajax({
        url: global_variables.site_url + "explore/get_listings",
        type: 'GET',
        dataType: 'json',
        data: {
            'province' : province,
            'city' : city,
            'category' : category,
            'slug' : slug,
            'order_by' : order_by,
            'limit' : limit,
            'offset' : offset
        }
    }).done(function(response) {     
         console.log(response); 
    }).fail(function(response){
         console.log(response);  
    });
  }
}

The output of console.log is:

{listings: null}

This is my Controller function:

public function get_listings(){
    header("Content-Type: application/json");

    $province = $this->input->get('province', true);
    $city = $this->input->get('city', true);
    $category = $this->input->get('category', true);
    $slug = $this->input->get('slug', true);
    $order_by = $this->input->get('order_by', true);
    $limit = $this->input->get('limit', true);
    $offset = $this->input->get('offset', true);

    $data['listings'] = $this->listing_model->get_listings($province, $city, $category, $slug, 
                                                           $order_by, $limit, $offset);
    echo json_encode($data);
    exit;
}

The output of my controller is:

{"listings":[{"listing_id":"14","listing_title":"Listing One","listing_slug":"listing-one","listing_description":"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).","created_at":"2019-11-16 15:28:02"},{"listing_id":"15","listing_title":"Listing Two","listing_slug":"listing-two","listing_description":"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).","created_at":"2019-11-16 15:28:02"},{"listing_id":"16","listing_title":"Listing Three","listing_slug":"listing-three","listing_description":"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).","created_at":"2019-11-16 15:28:02"},{"listing_id":"17","listing_title":"Listing Four","listing_slug":"listing-four","listing_description":"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).","created_at":"2019-11-16 15:28:02"},{"listing_id":"18","listing_title":"Listing Five","listing_slug":"listing-five","listing_description":"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).","created_at":"2019-11-16 15:28:02"}]}

This is my module function, which is called by the controller:

public function get_listings($province = FALSE, $city = FALSE, $category = FALSE, $slug = FALSE, 
                             $order_by = FALSE, $limit = FALSE, $offset = FALSE){
     if($limit){
        $this->db->limit($limit, $offset);
     }

     if($slug == FALSE){
        $this->db->select('*');
        $this->db->from('listings');

        $query = $this->db->get();
        return $query->result_array();
     }
}

The JSON displayed in my controller is correct, but it seems like all data is lost when arriving at the Ajax function, unless I'm not displaying or calling the data correct inside of my Ajax function. Your assistance would be appreciated!

1
  • please show the output of console.dir(response) Commented Nov 17, 2019 at 12:36

1 Answer 1

2

The listing_model::get_listings(...) method does not always explicitly return a value: when $slug is not false, it will not explicitly return anything, thus in that case the return value will be null. Most probably that's the reason.

Sign up to request clarification or add additional context in comments.

3 Comments

@Quintin and in your controller, you can always use print_r($data['listings'])?to debug/check values of the return from your model...
@Zoli I do understand what you are saying, I did not post all of the code of my model function, since in this scenario everything including $slug is definitely false. The problem is not with the model, because in the Controller I do get data back from the model which in my answer I printed it to the screen in JSON format. The problem is between the Controller and my Ajax call, when the data reaches the Ajax it seems like it went missing.
@Vickel please review my question again, I did output the result of my model. I definitely do get data back from my model and in valid JSON format. The problem is somewhere between my Controller and Ajax call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.