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

时间:2016-09-23 12:05:37

标签: angularjs ajax codeigniter-3

如何从角度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 个答案:

答案 0 :(得分:0)

  1. 看起来像使用角度js获取json对象的codeigniter请求

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

      功能(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);
                 });
         }