和AngularJS的帖子没用。

时间:2022-08-23 13:08:01

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

我想向我的API发送一个post请求。它使用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 :

但这与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>

3 个解决方案

#1


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

在HTML页面中的用法

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

#2


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.

使用AngularJS就像使用jQuery一样。它不是。AngularJS使用依赖注入,所以需要将$http调用包装在控制器中。

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

你应该读一下盎格鲁人。一些有用的链接:

#3


0  

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

不好的是,我的问题来自于我在php中的后端,我只是得到了我的数据:

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

and not with $_POST

而不是$ _POST

#1


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

在HTML页面中的用法

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

#2


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.

使用AngularJS就像使用jQuery一样。它不是。AngularJS使用依赖注入,所以需要将$http调用包装在控制器中。

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

你应该读一下盎格鲁人。一些有用的链接:

#3


0  

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

不好的是,我的问题来自于我在php中的后端,我只是得到了我的数据:

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

and not with $_POST

而不是$ _POST