dongzhouji4021 2015-11-18 00:00
浏览 37

将json从javascript发送到codeigniter中的控制器

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度