我有来自ajax调用的ajax json数据,响应是json_encode。
public function editimg()`enter code here`
{
$config['image_library'] = 'gd2';
$id=$this->input->post('hiddenimgid');
$config['upload_path'] = './assets/img/movies';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '150';
$config['max_width'] = '199';
$config['max_height'] = '199';
//echo "hii";die;
// print_r($config);die;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
//echo "not uploaded";die;
$error = array('error' => $this->upload->display_errors());
$data['message'] = "Image Cannot be Uploaded,Try Again !!";
$data['error']=$error;
$this->index_one($data);
} //if
else
{
//echo "hii";die;
$config['image_library'] = 'gd2';
$image_data = $this->upload->data();
$filename = $image_data['file_name'];
$source_path = 'assets/img/movies/'.$filename ;
$new_image_path = 'assets/img/movies/'.$image_data['raw_name'].'_thumb'.$image_data['file_ext']; //name of resized image
$target_path = 'assets/img/movies/';
$thumb['image_library'] = 'gd2';
$thumb['source_image'] = $source_path;
$thumb['new_image'] = $target_path;
$thumb['create_thumb'] = TRUE;
$thumb['maintain_ratio'] = TRUE;
$thumb['width'] = 140;
$thumb['height'] = 200;
//print_r($thumb);die;
//$this->load->library('image_lib', $config);
$this->load->library('image_lib', $thumb);
if ( !$this->image_lib->resize())
{
//echo "hii";die;
//$this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));
$this->session->set_flashdata('errors', $error= $this->image_lib->display_errors('', ''));
print_r($error);die;
}
else
{
//echo "hii";die;
//print_r($thumb);die;
$data=array(
'image'=>$new_image_path
);
//print_r($new_image_path);die;
if($this->movies_model->updateMovies($data,$id))
{
$data['message'] = "Movies added successfully !!";
//$this->index_one($data);
redirect("movies/index", 'refresh');
//print_r($thumb);die;
}
else
{
$data['message'] = "not_uploaded !!";
$this->index_one($data);
}
}
}
}
我正在访问它并放入数据表中。 我有脚本代码
$user=array('id'=>$val->id,'username'=>$name,'contactno'=>$val->contact_no);
echo json_encode($user);
我无法在datatable中获取json数据。请帮忙
答案 0 :(得分:0)
使用datatable ajax语法
$('#example').dataTable( {
"ajax": {
"url": "<?php echo base_url(); ?>list/people",
"type": "POST"
}
});
PHP:
$user =[];
$content_array = $this->list_model->users_list();
foreach($content_array as $key=>$val) {
$id = $val->id;
$name = $val->user_name;
$contact=>$val->contact_no;
$user[]=array('id'=>$val->id,'username'=>$name,'contactno'=>$val->contact_no);
}
echo json_encode(array("data"=>$user));
答案 1 :(得分:0)
您可以将ajax DataTable用于此
$(document).ready(function() {
$('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax":{
url : '<?php echo base_url(); ?>list/people'
type: "post", // method , by default get,
data : function ( d ) {
//any data that you want to send with request
/*
var dt_params = {'myKey' : 'MyValue'};
$.extend(d, dt_params);
*/
},
complete : function(response){
// you will receive data here
}
}
});
});
来自服务器的数据与
相同$用户=阵列(&#39; ID&#39; =&GT; $ val-&GT; ID&#39;用户名&#39; =&GT; $名称,&#39; contactno&#39; =&GT ; $ val-&GT; contact_no); echo json_encode($ user); exit;
答案 2 :(得分:0)
您的数组必须采用此格式
$user = array(
array('id'=>$val->id,'username'=>$name,'contactno'=>$val->contact_no)
);
因为您正在使用数据表。
数据表属性&#39;数据&#39;在javascript中必须是这样的格式
[{
'id': 1,
'username': 'my username',
'contact_no': 'my contact no.'
}]