【问题标题】:Ajax call to PHP and return array with json_encode() always nullAjax 调用 PHP 并返回带有 json_encode() 的数组始终为空
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • @Quintin 在您的控制器中,您始终可以使用print_r($data['listings'])?来调试/检查模型返回的值...
  • @Zoli 我明白你在说什么,我没有发布我的模型函数的所有代码,因为在这种情况下,包括 $slug 在内的所有内容都绝对是错误的。问题不在于模型,因为在控制器中,我确实从模型中获取了数据,在我的回答中,我以 JSON 格式将其打印到屏幕上。问题出在控制器和我的 Ajax 调用之间,当数据到达 Ajax 时,它似乎丢失了。
  • @Vickel 请再次查看我的问题,我确实输出了我的模型的结果。我肯定会从我的模型中以有效的 JSON 格式取回数据。问题出在我的 Controller 和 Ajax 调用之间。
猜你喜欢
  • 2016-06-02
  • 2018-07-13
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 2016-02-24
  • 2015-03-26
  • 2017-05-21
  • 1970-01-01
相关资源
最近更新 更多