AngularJS HTTP Message for PHP

I am sending some data to the page phpfrom the controller angularjsusing the method $httpas 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 that I can get this data in phpusing:

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

But I was wondering if there is a better way to angularjs, so I could use easy- $_POSTin phpto extract the data.

In jQuerywe could just use $.post, and they are available for phpin $_POST. Why angularjsis it not so simple. Is there a way to fill $_POSTout phpwhen we make a request $http postto angularjs. Please, help.

+4
3

php known.
jQuery $.param . .

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 :

username=Name&password=My%20Password

JSFiddle

+5

jQuery $.param. vp_arth. , -. , .

$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

, : jQuery , angular , , $_post

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

,

+1

Source: https://habr.com/ru/post/1614577/


All Articles