0

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.

4
  • What error message are you getting?
    – simeg
    Commented Apr 25, 2015 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 Commented Apr 25, 2015 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
    Commented Apr 25, 2015 at 16:33
  • Apart from that, there was a CORS issue even though I applied a filter. Following solution worked for that too. stackoverflow.com/questions/9310112/… Thanks!
    – emredmrl
    Commented Apr 25, 2015 at 20:38

1 Answer 1

1

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;
    });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.