简体   繁体   中英

Ajax call to PHP and return array with json_encode() always null

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!

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.

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