How to get the data in Struts from AngularJS post

前端 未结 3 1927
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 13:42

I’m using Struts1.2 with Angular to post some data and want to get the data in java.

I’m able to retrieve the data from the server and able to display it in the scre

相关标签:
3条回答
  • 2020-12-15 14:20

    It is not data that you should use params instead of data tag.

    $scope.postData = function() {
    
     var data = escape(angular.toJson($scope.items));
    
     $http({
          method: 'POST',
          url: '/StrutsWithAngular/shopingCart.do',
          params: 'cartValues='+data,
          headers: {'Content-Type': 'application/x-www-form-urlencoded'}
       }).success(function(data, status, headers, config) {
            $scope.items = data;
       }).error(function(data, status, headers, config) {
        // alert("Error :: "+data);
       });
    };
    
    0 讨论(0)
  • 2020-12-15 14:30

    Using the second try, before posting the data convert scope to json

    This can be done either in two ways either using JSON api or Angular api

    I used angular.toJson() and I also used escape method for accepting special characters.

    Using request.getParameter you can get the value in the sever side. Hope solution helps everybody.

    // Second Try

    $scope.postData = function() {
    
         var data = escape(angular.toJson($scope.items));
    
         $http({
              method: 'POST',
              url: '/StrutsWithAngular/shopingCart.do',
              data: 'cartValues='+data,
              headers: {'Content-Type': 'application/x-www-form-urlencoded'}
           }).success(function(data, status, headers, config) {
                $scope.items = data;
           }).error(function(data, status, headers, config) {
            // alert("Error :: "+data);
           });
    };
    
    0 讨论(0)
  • 2020-12-15 14:45

    Angular does not POST form data, it transmits a JSON object to the server. For information on JSON itself, see JSON.org. There are also a wealth of libraries listed on that page (towards the bottom) that let you parse JSON in various languages, including Java. Not being a Java developer, I can't say that one of them is better than the other, but you should be able to get a good idea of suitability by reading the docs on each one.

    0 讨论(0)
提交回复
热议问题