繁体   English   中英

Ajax 调用 PHP 并返回带有 json_encode() 的数组总是 null

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

我是 CodeIgniter 的新手,还在学习。 我正在尝试对我的一个控制器进行 Ajax 调用,该控制器将从模块返回数据,然后将数据回显到包装在 json_encode() 中的 Ajax。 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.

这是我的 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);  
    });
  }
}

console.log 的 output 为:

{listings: null}

这是我的 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;
}

我的 controller 的 output 是:

{"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"}]}

这是我的模块 function,由 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. 您的帮助将不胜感激!

listing_model::get_listings(...)方法并不总是显式返回一个值:当$slug不是false时,它不会显式返回任何内容,因此在这种情况下返回值将是null 很可能就是这个原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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