0

I'm newer in angular js and I would like to call the web-service (implemented with Spring REST ) which use multiple data in his request body but i didn't now how the parsing can be done. Can someone help me please ?

This is my Spring method in controller:

@RequestMapping(value = "/addpost", method = RequestMethod.POST, headers = "Accept=application/json")
    public @ResponseBody String addpost(@RequestBody PostDto post, @RequestBody UserDto user) {
        postservice.addPost(post, user);
        return "post inserted";

    }

This is how i tried to call this method

$scope.addPost=function(x,y){

        $http.post('http://localhost:8080/wall/addpost',[x,y]).
          success(function(data, status, headers, config) {
              $scope.persons.push(data);
             $scope.user="";
             $scope.post="";
          }).
          error(function(data, status, headers, config) {
              alert("erreur");

          })
housseminfo
  • 97
  • 4
  • 12

3 Answers3

1

If you look $http API, you will find that $http.post second parameter contain json of object which we want to post to the server. So that could {post: x, user: y} & then JSON.stringify() that json & pass it to server. sending array in data will won't be understand by your server side method.

Code

$scope.addPost = function(x, y) {

  $http.post('http://localhost:8080/wall/addpost', {
    post: x, //assuming x is post
    user: y //assuming y is use r
  }, headers: {
             'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
             }).
  success(function(data, status, headers, config) {
    $scope.persons.push(data);
    $scope.user = "";
    $scope.post = "";
  }).
  error(function(data, status, headers, config) {
    alert("erreur");
  })
}

Hope this could help you, Thanks.

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
1

You need to send across the data using application/x-www-form-urlencoded.

$http({
    method: 'POST',
    url: 'http://localhost:8080/wall/addpost',
    data: $.param({post: x, user: y}),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})

See this answer for more details How can I post data as form data instead of a request payload?

Also your function definition should be

@RequestMapping(value = "/addpost", method = RequestMethod.POST, headers = "Accept=application/json")
    public @ResponseBody String addpost(@RequestParam PostDto post, @RequestParam UserDto user) {
        postservice.addPost(post, user);
        return "post inserted";

    }
Community
  • 1
  • 1
thedoctor
  • 1,498
  • 1
  • 12
  • 12
  • how can i do it in my case ? @thedoctor – housseminfo Feb 26 '15 at 15:04
  • $ is not defined should i inject it ? – housseminfo Feb 26 '15 at 15:48
  • Can you load a full version of jquery before loading angularjs, the $.param comes from jquery. – thedoctor Feb 26 '15 at 15:57
  • i downloaaded it the $.param work well now but still a problem HTTP code error 415 type not supported by the server – housseminfo Feb 26 '15 at 16:16
  • Have a look at the server logs to see if there was an exception, also see this answer to see if it's relevant http://stackoverflow.com/questions/21344352/json-plus-spring-mvc-3-2-error-415-unsupported-media-type. Also can you try and take off headers section on the annotation in your java code as you're not sending the overall request in JSON format. – thedoctor Feb 26 '15 at 16:25
-1

AngularJS handles POST's differently than what you're probably used to (jQuery for example). One thing I do, is set up a default way for app's .config() to handle $http requests, by defaulting headers to application/x-www-form-urlencoded and also serializing objects before a request is made.

angular.module('yourApp', [])

    .config(['$httpProvider', function ($httpProvider) {

        // default header
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

        // using transformRequest this gets handled before each $http execution

        $httpProvider.defaults.transformRequest = function (data) {
            if (data === undefined) {
                return data;
            }

            return angular.isObject(data) && String(data) !== '[object File]' 
                ? $.param(data) // serialize it if it's an object
                : data;         // otherwise leave it alone
        };
    });

Now you'll be able to use it like you always do:

$http.post('url', { /* your data in an object */ });
Mark Pieszak - Trilon.io
  • 61,391
  • 14
  • 82
  • 96