【发布时间】:2019-11-16 16:54:46
【问题描述】:
我是 CodeIgniter 的新手,还在学习。我正在尝试对我的一个控制器进行 Ajax 调用,该控制器将从模块返回数据,然后将数据回显到包装在 json_encode() 中的 Ajax。当我在控制器中回显数据时,它会输出有效的 JSON,但是当我在 Ajax 函数中使用 console.log() 时,结果始终为空。
这是我的 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 的输出是:
{listings: null}
这是我的控制器功能:
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;
}
我的控制器的输出是:
{"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"}]}
这是我的模块函数,由控制器调用:
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();
}
}
我的控制器中显示的 JSON 是正确的,但在到达 Ajax 函数时似乎所有数据都丢失了,除非我没有在我的 Ajax 函数中正确显示或调用数据。我们将不胜感激!
【问题讨论】:
-
请显示console.dir(response)的输出
标签: php jquery json ajax codeigniter