如何从 angularJS 接收数组 post 到控制器中的 codeigniter 并保存到数据库中

how to received array post from angularJS to codeigniter in controller and save into database

如何从 angular JS 中的 post 数组接收数据到 Codeigniter 中?,下面是我的代码

Codeigniter 中的控制器

     public function add_data_chat()
            {


            $datas = json_decode(file_get_contents('php://input'), TRUE);



                $message                 =   $datas ["message"];
                $payment_step         =   $datas ["payment_step"];
                $negotiation_price    =  $datas ["negotiation_price"];
                $room_id                 =  $datas["room_id"];
                $member_id           =  $datas["member_id"];


         $sql="INSERT INTO dash_chat_documentation (message, payment_step, negotiation_price, room_id, member_id, chat_time) VALUE ('$message','$payment_step',$negotiation_price,'$room_id',$member_id,now())";

            $data= $this->db->query($sql);
}

ANGULARjs

$scope.tambahData = function(){

            databaru = {

                message:$scope.databaru.message,
                payment_step:'regular_chat',
                negotiation_price:'0',
                room_id:'123',
                member_id:'01'
             };

            $http({
                method: 'POST',
                url: '<?php echo base_url('dashboard/workspace/add_data_chat') ?>',
                headers : {'Content-Type': 'application/x-www-form-urlencoded'},

                data: $scope.databaru
                }).success(function (hasil) { $scope.data.push({
                    message: $scope.databaru.message,
                    chat_time: $scope.databaru.chat_time
               });
                 $scope.databaru.message='';
                });
};

为什么我无法从 angularJS 接收数据到 codeigniter 的问题,请帮助

  1. 就像 angular js get json object

    的 codeigniter 请求
     $params = file_get_contents('php://input');
       if (empty($params)) {
           $parmete2 = $_POST;
       } else {
           $parmete2 = json_decode($params, true);
       }
    
    1. angular js post

    -

    函数(form_data){

             $scope.submitted = true;
    
             $scope.reg = form_data;
    
             var fd = new FormData();
    
             var file = $scope.fileToUpload;
    
             fd.append('file',file);
    
             fd.append('first_name',$scope.reg.first_name);
    
             fd.append('last_name',$scope.reg.last_name);
    
             $http.post(API_URL+"user_update_profile", fd, {
    
                transformRequest: angular.identity,
    
                headers: {'Content-Type': application/json}
               })
               .success(function(res){
                    console.log(res);
               })
               .error(function(err){
                    console.log(err);
               });
       }