$ http.post在控制台和提供的网址中发送为空

时间:2016-10-17 08:18:43

标签: javascript angularjs

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>AJAX with Struts 2 using AngularJS</title>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);

app.controller("MyController",function($scope, $http) {
$scope.getDataFromServer = function() {
        $http.get("angularAction").success(
                function(data, status, headers, config) {
                        $scope.person = data;
                }).error(function(data, status, headers, config) {
                     // called asynchronously if an error occurs
                     // or server returns response with an error status.
        });
};




$scope.postDataToServer=function(){
    /*$scope.items={"firstName":"sandeep","lastName":"suluguri"}; */
    /* var data = angular.toJson($scope.items); */
    /* var data=$scope.items; */

在控制台中,它打印为null,当我从angularPage获取数据时,甚至显示为null。我编写post方法的格式是否正确?

    $http({
         method: 'POST',
         url: 'angularPage',
         data: {'message' : 'harsha'},
         headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'}
      }).success(function(data, status, headers, config) {
         /*   $scope.items = data; */
           console.log(data);
      }).error(function(data, status, headers, config) {
       // alert("Error :: "+data);
          console.log('error');
      });   
}
});
</script>
</head>
<body>

单击按钮时,它会触发控制器中的相应功能。 我的get方法工作正常。它从该URL获取数据并显示在页面上

<div ng-app="myApp">
        <div ng-controller="MyController">
                <button ng-click="getDataFromServer()">
                Fetch data from server
                </button>
                <p>First Name : {{person.firstName}}</p>
                <p>Last Name : {{person.lastName}}</p>
                <button ng-click="postDataToServer()">post data to                      server</button>
        </div>
</div>
</body>
</html>

0 个答案:

没有答案