1

I would like to send a post request to my API. It works with jQuery :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
  $.ajax({
  type: "POST",
  url: "api.php?option=inscription",
  data: {lol : "mess"}
});
</script>

But it doesn't with AngularJS :

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">    </script>
{{1+1}}
<script>
$http.post('api.php?option=inscription', {lol : "mess2"})
.success(function(){alert('cool');});
</script>

If someone can help me. Thank you !

UPDATE : Thank for your answers, I wanted to simplify but it wasn't clear anymore. So with your help, this is my new code, and the problem is the same. The data in the backend is empty ;

frontend :

<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">   </script>

<div ng-controller="MainCtrl"></div>
{{data}}

<script>
var app = angular.module('myApp', []);

app.service('SomeService', function($http) {
this.readData = function(dataUrl, dataTobePosted) {

    var back =  $http.post(dataUrl, dataTobePosted);

    back.success(function(data){
      console.log(data);
      return data;
    }).error(function(data, status, headers, config) {
      return status;
});
    }
});

app.controller('MainCtrl', function($scope, $http, SomeService){
$scope.readData = function(url) {
    var dataTobePosted = {"lol": "mess"};

    $scope.data = SomeService.readData(url, dataTobePosted);
}

$scope.readData('api.php?option=inscription');
});
</script>
</html>
1
  • 1
    Did you injected $http in the controller ? @LekimR Commented May 5, 2015 at 10:51

3 Answers 3

2

For clarity, I am suggesting a simple implementation. However, further reading may needed in order to understand the behaviour precisely.

angular.module('myApp').service('SomeService', function($http) {
    this.readData = function(dataUrl, dataTobePosted) {
        // read data;
        return $http.post(dataUrl, dataTobePosted)
            .then(function(res) {
                return res.data;
            }, function(res) {
                return res;
            }
    }
    return this;
});

angular.module('myApp').controller('MyController', function($scope, SomeService) {
    $scope.readData = function(url) {
        var dataTobePosted = {"lol": "mess"};

        SomeService.readData(url, dataTobePosted)
        .then(function(res) {
            $scope.data = res;
        }, function(res) {
            // Display error
        }
    }

    $scope.readData('api.php?option=inscription');
}

Usage in the HTML page

<div ng-controller="MyController">
    {{data}}
</div>  
Sign up to request clarification or add additional context in comments.

Comments

1

You're using AngularJS as if it's jQuery. It's not. AngularJS works with dependency injection, so you need to wrap your $http call inside a controller.

You should probably read up on AngularJS. A few useful links:

Comments

0

My bad, my problem came from my backend in the php I just get my data with :

 $data = json_decode(file_get_contents("php://input"));

and not with $_POST

Comments

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.