php - Empty post response ajax -
i'm trying use ajax geta response codeigniter controller method doesn't work ,the response it's empty.
<script type="text/javascript"> $(document).ready(function(){ $("#crear").click(function() { //$('#error_msg').html("error"); $.ajax({ type:"post", url:"clase/create", success:function(data){ $('#error_msg').html(data); } }); }); }); </script>
and controller
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class clase extends ci_controller { function __construct() { parent::__construct(); $this->load->model('clase_model','',true); $this->load->helper(array('form')); } function index() { if($this->session->userdata('logged_in')){ $data['title'] = 'gestiĆ³n de clases'; $data['clases'] = $this->clase_model->getall(); $this->load->view('header', $data); $this->load->view('clase_view', $data); } else{ redirect('login', 'refresh'); } } function create(){ if($this->session->userdata('logged_in')){ $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'nombre', 'trim|min_length[2]|required'); $this->form_validation->set_rules('info', 'informaciĆ³n', 'trim'); if($this->form_validation->run() == false){ $data = array( 'error_message1' => form_error('name'), 'error_message2' => form_error('info') ); return $data['error_message1']; } else{ if($this->input->post('info')){ $this->insert2($this->input->post('name'),$this->input->post('info')); } else{ $this->insert1($this->input->post('name')); } } } else{ redirect('login', 'refresh'); } } function insert2($name,$information){ $dat = array( 'nombre'=>$name, 'info'=>$information ); $this-> db ->insert('clase',$dat); echo $name; redirect('clase', 'refresh'); } function insert1($name){ $dat = array( 'nombre'=>$name, ); $this-> db ->insert('clase',$dat); redirect('clase', 'refresh'); } } ?>
and response header
cache-control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 connection keep-alive content-length 0 content-type text/html; charset=utf-8 date sat, 25 apr 2015 16:04:47 gmt expires thu, 19 nov 1981 08:52:00 gmt keep-alive timeout=5, max=100 pragma no-cache server apache/2.4.10 (win32) openssl/1.0.1i php/5.6.3 set-cookie ci_session=941a601d7eaf9f590d21bd4c0aa8c2ac043faa81; expires=sat, 25-apr-2015 18:04:47 gmt; max-age=7200 ; path=/; httponly x-powered-by php/5.6.3
somebody can tell me wrong?it's first time ajax
to response in ajax request in codeigniter should use-
echo json_encode('your data');
Comments
Post a Comment