1

I have strange Codeigniter - Angular issue. My idea was to set up controller to work like this:

  • index is simple Angular page, just 1 app and 1 controller
  • get is get data from database
  • set is save data which are sent using $http.post

Problem is that $http.post() send proper data, I see it in Firebug, but CI doesn't catch that.

I change header parameters, tried to catch with file_get_contents("php://input") and $this->input->post, but always was Null.

Any idea?

I tried this:

$http(   
{
method: 'POST',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},

data: data
});

With and without headers and transformData changes... and nothing.

UPDATE

I tried to send same data to raw php script, and it's worked. so, somehow - Codeigniter blocking this.

Any idea ?

Community
  • 1
  • 1

1 Answers1

0

this is a common problem, lets begin: add ngRoute script to your document and in your app Module, add this code

var myApp = angular.module('myApp', ["ngRoute"]).
config(["$httpProvider",function($httpProvider) 
{
    $httpProvider.interceptors.push(['$q', function($q) {
    return {
            request: function(config) {
                if (config.data && typeof config.data === 'object') {
                    // Check https://gist.github.com/brunoscopelliti/7492579 
                    // for a possible way to implement the serialize function.
                    config.data = serialize(config.data);
                }
                return config || $q.when(config);
            }
        };
    }]);

    var serialize = function(obj, prefix) {
        // http://stackoverflow.com/questions/1714786/querystring-encoding-of-a-javascript-object
        var str = [];
        for(var p in obj) {
            var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
            str.push(typeof v == "object" ? serialize(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v));
        }
        return str.join("&");
    }

}]).
run(function($http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8;";

});

now on your postService (or wherever you are using $http)

var postData = function(method,data) {
   return $http.post("controller.php?/"+method,{data:data})
   .then(function(response) {                
        return response.data;                    
    }, 
    function(response) {        
      //handle error
    }); 
 }

//use
postData("userLogin",{username:"username",password:"password"}).then(function(response){

});

and now codeIgniter will get the input->post("data") (it's always data);

Daniel Krom
  • 9,751
  • 3
  • 43
  • 44
  • Tried that and not working: data[name] Delivery Name data[notes] data[ranges][0][from] 0 data[ranges][0][to] 0 data[showRanges] false data[url] http:// data[weight_from] 0 data[weight_to] 0 in POST, but on CI side Null – Milan Stojadinovic Aug 09 '15 at 09:36
  • well this need to see what you are doing, can you edit your post and show what you are doing in CI? – Daniel Krom Aug 09 '15 at 09:38
  • file_get_contents("php://input") also tried $this->input->post(); and when I try print_r() it returns Null (first) or Array() for second. – Milan Stojadinovic Aug 09 '15 at 20:14
  • try $this->input->post("data"); and print_r($_POST) – Daniel Krom Aug 10 '15 at 07:25
  • has this question been answered yet or not? I am working on the same situation right now. – aintno12u May 01 '16 at 08:09
  • 1
    Angular 1.5+ solved it automatically using `$http({ method:"POST", url : "api/"+apiMethod, headers: { 'Content-Type': "application/json" }, data : data })` – Daniel Krom May 01 '16 at 12:46