我在codeigniter中的新东西,所以我想从javascript发送一个json到ajax控制器这里它的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);
}
});
});
这是我的控制器代码:
<?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));
}
}
它在我做的方式中的可行性?或者还有其他方法可以做到吗?
THX