2

I am little bit frustrated with my below angular code, as it is not sending the JSON data to my spring boot based micro-service running in the same machine. Please help me out! FYI, I am a newbie to JavaScript world.

$scope.addBook = function() {
    var data = $.param({
        json: JSON.stringify({
            name: $scope.bookName,
            isbn: $scope.isbn,
            author: $scope.authorName,
            pages: $scope.pages
        })
    });

    var config = {
        headers : {
            'Content-Type': 'application/json'
        }
    }

    var result = $http.post('http://localhost:8080/book', data, config);
    result.success(function (data, status, headers, config) {
        $scope.result = JSON.stringify({data: data});
    });
    result.error(function (data, status, header, config) {
        $scope.result = JSON.stringify({data: data});
    });
};
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Rajib Deka
  • 551
  • 1
  • 7
  • 22

4 Answers4

3

You don't need to run JSON.stringify or $.param, just pass the object directly to $http.post(). Angular creates the JSON for you.

Prashant
  • 7,340
  • 2
  • 25
  • 24
1

Just pass your data object like this:

$scope.addBook = function() {
        var data = 
            {
                name: $scope.bookName,
                isbn: $scope.isbn,
                author: $scope.authorName,
                pages: $scope.page
        };

        var config = {
            headers : {
                'Content-Type': 'application/json'
            }
        };

        var result = $http.post('http://localhost:8080/book', data, config);
        result.success(function (data, status, headers, config) {
            $scope.result = JSON.stringify({data: data});
        });
        result.error(function (data, status, header, config) {
            $scope.result = JSON.stringify({data: data});
        });
    };

You don't need to stringify it. You probably can remove the stringify form the result you get too if your service is returning JSON.

maxshuty
  • 9,708
  • 13
  • 64
  • 77
nweg
  • 2,825
  • 3
  • 22
  • 30
  • I had another issue - XMLHttpRequest cannot load http://127.0.0.1:8080/book. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. Which I have fixed using this link : http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – Rajib Deka Mar 08 '16 at 19:32
0

I think $http does not send stingifyed data.

Try

$scope.addBook = function() {
    var data = {
            name: $scope.bookName,
            isbn: $scope.isbn,
            author: $scope.authorName,
            pages: $scope.pages
        };

    $http.post('http://localhost:8080/book', data)
        .success(function (data, status, headers, config) {
            $scope.result = JSON.stringify({data: data});
        })
        .result.error(function (data, status, header, config) {
            $scope.result = JSON.stringify({data: data});
        });
};
Matthias
  • 908
  • 1
  • 7
  • 22
0

Change your data to the following. It should work

var data = $.param({
                'name': $scope.bookName,
                'isbn': $scope.isbn,
                'author': $scope.authorName,
                'pages': $scope.pages
        });
amanpurohit
  • 1,246
  • 11
  • 19