4

I am posting some data to a php page from angularjs controller using the $http method as shown below.

$scope.login = function(){
    var data = {
        'username' : $scope.username,
        'password' : $scope.password
    };
    $http.post('login.php', data).success(function(response){
        console.log(response);
    });
};

I know I can retrieve this data in php using :

$postdata = json_decode(file_get_contents('php://input'), true);

But I was wondering if there is better way in angularjs so I could use the simple $_POST in php to retrieve the data.

In jQuery we could just simply use $.post and those are available for php in $_POST. Why for angularjs that is not that simple. Is there any way to populate $_POST of php when we do a $http post request in angularjs. Please help.

6
  • Possible duplicate of receive json post with php Commented Nov 5, 2015 at 9:02
  • is this in service?? Commented Nov 5, 2015 at 9:04
  • I checked the post you mentioned. But that post doesn't explain how to populate $_POST Commented Nov 5, 2015 at 9:05
  • @abdulla this is a simple controller file Commented Nov 5, 2015 at 9:07
  • 1
    You'll need to override $httpProvider.defaults.transformRequest to a transformer that serialises the data into a standard URL-encoded format instead of JSON. Read the documentation for some samples. That's just to point you in the right direction, if someone wants to develop this into a full answer by all means please do. Commented Nov 5, 2015 at 9:08

3 Answers 3

5

You need to convert your request to php known format.
I use jQuery $.param method to build it. You can write your own.

app.config(['$httpProvider', function($http) {  
    $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    $http.defaults.transformRequest = function(data) {
            return $.param(data);
        };
    }]);

urlencoded is followed simple format:

username=Name&password=My%20Password

JSFiddle

Sign up to request clarification or add additional context in comments.

6 Comments

getting an empty array when print_r($_POST)
I add fiddle for this
Be carefull with Content-Type capitalization. One already exists, and if you write it wrong - two headers will be sended. HTTP server selects one of this (undefined behavior)
@Adersh, success? Or still have issues?
I can't understand how the data is to be received in php for this method. via $_POST I am getting an empty array.
|
2

Got it working using jQuery $.param method. Thanks to vp_arth's answer. I just added this answer as it might help someone. This can be used if it is needed for only one request.

$scope.login = function(){
    var data = {
        'username' : $scope.username,
        'password' : $scope.password
    };

    $http({
        url: "login.php",
        method: "POST",
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        data: $.param(data)
    }).success(function(response){
        console.log(response);
    });

};

2 Comments

It usable if you have only one request. I strongly recommend to use transformRequest, if you have more than one post request in your project. It left your code clean and readable, like $http.post(url, data)
yes,..you are correct. I have accepted and voted up your answer :) just added this answer as it might help someone.
1

well I just use this in my controller class, so all my post requests: normal ones with jQuery or the ones through angular all end up being in $_post

    if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty( $_POST ))
        $_POST=$_REQUEST = json_decode( file_get_contents( 'php://input' ), true );

have fun,

4 Comments

More correct to check Content-type header as I wrote in comment above. To be sure, that client sends json.
@vp_arth, not necessarily, if $_post is empty so it's from angular, no need for more complexity here.
It looks more complex for me now. empty($_POST) is incorrect and magic. It can be empty in other cases. Also what should we doing with other queries, like PUT or PATCH?
@vp_arth OP asked about POST, for other cases you could use a switch statement. $_post is an array so we can always check it with the empty function, why incorrect??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.