im new in codeigniter so i wanna send a json from javascript to a controller usin ajax here its the code of javascript
var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" },'+
'{ "algo": "7"} ]}';
var obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[3].algo;
$(document).on("click", "#boton", function(event){
event.preventDefault();
var id = $(this).val();
$.ajax({
type : "POST",
url : window.location.protocol + "//" + window.location.host + "/css/index/index/click",
data : "{'places':'" + obj + "'}",
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(data) {
alert(data);
},
fail : function(data) {
alert(data);
}
});
});
and here is my code of my controller:
<?php
class Index extends CI_Controller {
function __construct(){
parent::__construct();
}
public function Index()
{
$this -> load -> view('index/principal');
}
public function click(){
$places = $this->input->post('places');
print_r(json_decode($places));
}
}
its posible in the way that im doing? or there is other way to do it?
thx