php - How to receive ajax json data in a codeigniter controller -


on client side have :

var postdata = {     "id" : id,     "message" : message };  console.log(postdata);  $.ajax({     type: "post",     url: "controller/function",     data: postdata,      success: function(){         alert(id + ' ' + message);     } }); 

this appears working can see correct post parameters in chrome dev tools. in codeigniter controller have tried:

echo 'postid' . $_post['id'].' '.$_post['message'];  $postdata=$this->input->post('id');  var_dump($postdata); exit; 

i'm getting:

message: undefined index: id message: undefined index: message  boolean(false) 

the $_post array empty.

how can fix this? thank help

you may add datatype:'json' in ajax options

$.ajax({        type: "post",        url: "controller/function",        data: postdata,        datatype:'json',        success: function(){            alert(id + ' ' + message);        } }); 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -