Angluarjs http.post不起作用

时间:2015-07-20 11:52:04

标签: angularjs http

这是我与Angluar.js的第一个项目,我试图发布一个对象(personne),但它没有用。我的代码有问题吗?

myapp.controller('personCtrl', function ($scope, $http) {

$scope.connexion = function(personne) {
    $http.post("http://localhost:8080/app/personne/testpost",$scope.personne)
        .success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
        })
        .error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;
        });

    console.log('ca fonctionne');
});

2 个答案:

答案 0 :(得分:0)

尝试这个。

$scope.connexion = function(personne) {
    $http.post("http://localhost:8080/app/personne/testpost",{personne : $scope.personne}).
        success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
        }).
        error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;
        });

    console.log('ca fonctionne');
};

答案 1 :(得分:0)

您在帖子请求中传递$scope作为参数,但您需要将参数作为对象变量传递:

myapp.controller('personCtrl', function ($scope, $http) {

$scope.connexion = function(personne) {
    $http.post("http://localhost:8080/app/personne/testpost",{persone: $scope.personne})
        .success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
        })
        .error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;
        });

    console.log('ca fonctionne');
});

检查$http的角度文档。