0

This is AngularJS Controller

'use strict';

/**
 * @ngdoc function
 * @name demonApp.controller:totalTasty.becameAChefCtrl
 * @description
 * # MainCtrl
 * Controller of the demonApp
 */
angular.module('demonApp')
  .controller('becameAChefCtrl', function($scope, $http) {
      // create a blank object to handle form data.
       $http.defaults.headers.common['Access-Control-Allow-Origin'] = '*';

      // calling our submit function.
        $scope.submitForm = function() {
        // Posting data to php file
        $http({
          method  : 'POST',
          url     : 'http://something/chef',
          data : {name: "as", meaning: "ass", gender: "asss", religion: "sdad"},
          headers : {'Content-Type': 'application/x-www-form-urlencoded'} 

         })
         .then(function mySuccess(response) {
      $scope.message = response.data;
    }, function myError(response) {
      $scope.errorName = response.statusText;

       });
     };
  });

My REST Service is in php on codeigniter :

<?php
header("Access-Control-Allow-Origin: *");
defined('BASEPATH') OR exit('No direct cripts are allowed here');
require(APPPATH.'libraries/REST_Controller.php');

class FirstRest extends REST_Controller {
  public function __construct(){
    parent::__construct();
    //calling model.
    $this->load->model("Babymodel","a");
    $this->load->helper('url');
    $this->output->set_header('Access-Control-Allow-Origin: *');
   }    

   public function chef_post(){
   $data = array(
  'name' =>$this->input->post('name'),
  'meaning' =>$this->input->post('meaning'),
  'gender' =>$this->input->post('gender'),
  'religion' =>$this->input->post('religion')
  );

      $value = $this->a->insertbabyData('baby',$data);
      if($value)

       {
           $this->response($value, 200); // 200 being the HTTP response code
       }
       else
       {
           $this->response(NULL, 404);
       }
    }     
 }
 ?>

And Method insertbabyData() in Model is:

public function insertbabyData($table_name,$data)
{

    $this->db->insert($table_name,$data);

    return $this->db->get_where($table_name,array('id'=>$this->db-> insert_id()))->row_array();

}

Notes: I using same server to run AngularJS project and RESTful services. My GET method work properly in same project with same REST for GET url.

1 Answers1

0

I got the solution:

Please add this with $http({.....});

transformRequest: function(obj) {
        var str = [];
        for(var p in obj)
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    },

And data will be:

 data: {name: "akash", meaning: "space", gender: "male", religion:"hindu"}