Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

This is my web api method:

    // POST api/Account/Register
    [AllowAnonymous]
    [Route("Register")]
    public async Task<HttpResponseMessage> Register([FromBody] string data)
    {
      //data is always null 
    }

AngularJs side:

        try {
            var postObj = new Object();
            postObj.UserName = $scope.RFId;
            ... Shortened for clarity...
            postObj.UserType = $scope.userType;

            //var validJson = JSON.stringify(postObj, "\t");
            //validJson = '"' + validJson + '"';
            postObj = '"' + postObj + '"';

            $http({
                method: 'POST',
                url: 'http://localhost:65337/api/Account/Register',
                data: postObj,
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(function (result) {
                console.log(result);
            }, function (error) {
                console.log(error);
            });

Result: WebApi does not recognize any of the data or returns 0.... and this is what fiddler catches as my post:

POST http://localhost:65337/api/Account/Register HTTP/1.1
Host: localhost:65337
Connection: keep-alive
Content-Length: 82
Accept: application/json, text/plain, */*
Origin: http://localhost:65337
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)           Chrome/39.0.2171.65 Safari/537.36
Content-Type: application/json
Referer: http://localhost:65337/WebApp/index.html
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

0=%22&1=%5B&2=o&3=b&4=j&5=e&6=c&7=t&8=+&9=O&10=b&11=j&12=e&13=c&14=t&15=%5D&16=%22 // <- Not sure what my post data is formatted like so...

Any help or directions would be greatly appreciated. Thanks

share|improve this question
1  
Assign data a json object not the content from json.stringfy. $http will stringify internally. Do not apply quotes. –  Chandermani Nov 25 '14 at 3:49
    
I have tried assigning postObj to data, without stringify or strings with no luck, but is postObj a correct Json object? If not, any suggestions? –  OverMars Nov 30 '14 at 1:08

1 Answer 1

up vote 0 down vote accepted

You need to encode your data, look at my article about how to use Web API 2 with AngularJS.

share|improve this answer
    
Great post, qq, I am sending username/pw as my data so I cannot send the data by URL encoding, is there no way for WebAPI to pull the data from the $http.Post body? –  OverMars Nov 30 '14 at 1:19
1  
see this article, it should help you find what you do wrong asp.net/web-api/overview/security/… –  Omar.Alani Dec 1 '14 at 2:53

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.