【问题标题】:Codeigniter null values ajax requestCodeigniter空值ajax请求
【发布时间】:2017-02-13 05:58:24
【问题描述】:

,我正在尝试在 codeigniter 的 ajax 请求中从数据库中获取一些值...但是当我放置 console.log 时 json 对象返回 null ([])...我需要帮助!!

JAVASCRIPT

function list_president() {
        var section = "1";
        $.post(baseurl + 'votos/load_politic', section,
            function(data) {
                console.log(data);
            });
    }

控制器

public function load_politic()
{
if ($this->input->is_ajax_request()) {

      $section = $this->input->post('section');
    $result  = $this->politic->get_president($section);

    echo json_encode($result);
  }
}

型号

public function get_president($section){


  $this->db->select("p.POLITIC_NAME, p.POLITIC_LASTNAME, p.POLITIC_SIDE, p.POLITIC_CHARGE");
  $this->db->from("politics p");
  $this->db->where("SECTION_ID",$section);

  $result= $this->db->get();

  return $result->result();
}

感谢您的帮助!

【问题讨论】:

  • 如果你是通过 id 获取的,那么你正在获取一行,所以你需要返回 $result->row()。在此之后,请在你的 $.post 调用中只调用 console.log(data) 并告诉我你得到了什么。
  • @Franco 我改变了 $result->result(); ...对于 $result->row() ...并返回 null =(
  • @charlietfl 为您指明了正确的方向,这引起了我的注意,因为我总是使用 ajax cal 来处理这类事情。但我希望他的建议已经解决了你的问题。

标签: php jquery json ajax codeigniter


【解决方案1】:

您没有向服务器发送键/值对...只是一个值。

所以没有$_POST['section'] 这基本上就是$this->input->post('section'); 是什么

尝试改变

 var section = "1";

 var section = {section: "1"};

您也没有验证发送的内容或检查 $result 是否返回任何内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    相关资源
    最近更新 更多