【问题标题】:Codeigniter, Ajax: Pass the variable type date from view in controllerCodeigniter,Ajax:从控制器的视图中传递变量类型日期
【发布时间】:2019-11-11 12:07:57
【问题描述】:

我想使用 Ajax 通过 post 方法将变量从视图发送到控制器。我的代码不起作用。有人可以帮我吗?

我的观点:变量随 daterangepicker 变化

var v_start_date = moment().subtract(7, 'days').format("YYYY-MM-DD HH:mm");
var v_end_date = moment().format("YYYY-MM-DD HH:mm");
...
//window.location.replace("<?php echo site_url('consum/ajax_excel');?>" + "/" + v_start_date + "/" + v_end_date);
$.ajax({
    url: "<?php echo site_url('consum/ajax_excel')?>",
    type: "POST",
    dataType : "JSON",
    data:  {
       start_date: v_start_date,
       end_date: v_end_date
    },
    success:function(data) {
    },
    error : function(data) {
    }
});

...

我的控制器:

public function ajax_excel() {
    //$start_date = $this->uri->segment(5);
    //$end_date = $this->uri->segment(6);
    $start_date = $this->input->post('start_date');
    $end_date = $this->input->post('end_date');
    $this->load->library('excel');
    $mobiledata = $this->consum->excel_list($start_date,$end_date);
    $objPHPExcel = new PHPExcel();

...

我的模特:

public function excel_list($start_date,$end_date){
    $this->db->select(array('date','consumator','produs','pret','cantitate','total','stare','utilizator'));
    $this->db->from('consum as m');
    if($start_date !="") if ($end_date !="") {
        $this->db->where('date >=', $start_date);
        $this->db->where('date <=', $end_date);
    }
    $this->db->order_by("date", "desc");
    $query = $this->db->get();
    return $query->result_array();
}

非常感谢!

【问题讨论】:

  • 我的代码不起作用..解释一下。你有什么错误吗?
  • 我没有阅读错误。使用它的分段方法。我认为当我通过 ajax 发送时它不会读取变量
  • 查看控制台检查是否有任何错误发生
  • error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); }
  • 200 and SyntaxError: Unexpected token � in JSON at position 0

标签: ajax database codeigniter post


【解决方案1】:

首先 print_r($_POST) 在您的控制器中,以确保没有拼写错误或者是因为编码等。如果没有打印任何内容,请检查配置文件夹中的 routes.php。如果您强制 CI 从特定路由进行路由,您可能需要定义一个新路由。

【讨论】:

  • 您好,感谢您的回复,我更改了 dataType : "text" 当我在控制器中有函数时 'public function ajax_excel() { $start_date = $this->input->post('start_date '); $end_date = $this->input->post('end_date'); print_r($_POST); print_r($start_date); print_r($end_date); }' 显示一个数组并很好地分隔变量。
  • 但是当我在控制器中有函数时` public function ajax_excel() { $start_date = $this->input->post('start_date'); $end_date = $this->input->post('end_date'); $this->load->library('excel'); $mobiledata = $this->consum->excel_list($start_date,$end_date); $objPHPExcel = 新 PHPExcel(); ... $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output');返回空值; ` 将变量与 excel 文件的内容混合。似乎是这样。有什么可以做的吗?非常感谢!
  • 控制器中的函数必须用PHPExcel库导出一个excel文件
猜你喜欢
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
相关资源
最近更新 更多