Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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 ?

share|improve this question

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);

share|improve this answer
    
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 9 '15 at 9: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 9 '15 at 9: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 9 '15 at 20:14
    
try $this->input->post("data"); and print_r($_POST) – Daniel Krom Aug 10 '15 at 7:25
    
has this question been answered yet or not? I am working on the same situation right now. – aintno12u May 1 at 8:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.