1

I am new to AngularJS. I am trying to post data to a local server. Below is the code.

        var postObject = new Object();
        postObject.email = "emailtobesaved1";
        postObject.userData ="userDatatobeSaved";
        var s=JSON.stringify(postObject);

        $http({
            url: 'http://localhost:8080/people',
            dataType: 'json',
            method: 'POST',
            data: s,
            headers: {
                "Content-Type": "application/json"
            }
        }).success(function(response){
            $scope.response = response;
        }).error(function(error){
            $scope.error = error;
        });

This data is not posted. Am I missing something? I activated proper CORS filter in the server side. Server side is a Spring boot application.

Thanks in advance.

emredmrl
  • 103
  • 1
  • 3
  • 9
  • What error message are you getting? – simeg Apr 25 '15 at 15:47
  • 1
    You're sending data as a string but you're setting the content-type header to application/json, so most likely your server can't handle that – Robin-Hoodie Apr 25 '15 at 15:57
  • 1
    Inspect the actual request in browser dev tools network tab. Need the clues from to debug further with. Why are you using JSON.stringify()? – charlietfl Apr 25 '15 at 16:33
  • Apart from that, there was a CORS issue even though I applied a filter. Following solution worked for that too. http://stackoverflow.com/questions/9310112/why-am-i-seeing-an-origin-is-not-allowed-by-access-control-allow-origin-error Thanks! – emredmrl Apr 25 '15 at 20:38

1 Answers1

2

You should not convert your data object to a string and send it. Send it as json object itself

Try this code instead:

var postObject = new Object();
    postObject.email = "emailtobesaved1";
    postObject.userData ="userDatatobeSaved";
    //var s=JSON.stringify(postObject);

    $http({
        url: 'http://localhost:8080/people',
        dataType: 'json',
        method: 'POST',
        data: postObject,
        headers: {
            "Content-Type": "application/json"
        }
    }).success(function(response){
        $scope.response = response;
    }).error(function(error){
        $scope.error = error;
    });
shivas
  • 913
  • 10
  • 15