Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
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 – NexusDuck 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. stackoverflow.com/questions/9310112/… Thanks! – emredmrl Apr 25 '15 at 20:38
up vote 1 down vote accepted

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;
    });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.