0

I have that code in the angularJS factory:

   myFactory.addNC = function (n) {
        return $http.post('/api/addNC/', { c: n }).then(function (data) {
            return data;
        });
    }

MVC ApiController:

[HttpPost]
public void addNC([FromUri]string c = null)
{
   ...
}

but 'c' param is null, why ? I see in FireBug that the value is being posted.

Also, if I declare the param like public void addNewCat([FromUri]string c) I get the 404 error - why ?

11
  • Wouldn't you want [FromBody] for a POST? Commented Nov 22, 2014 at 11:54
  • then I get the 500 error: An error has occurred.","ExceptionMessage":"Optional parameter 'c' is not supported by 'FormatterParameterBinding'.","ExceptionType":"System.InvalidOperationException Commented Nov 22, 2014 at 12:01
  • Oh it's just a string. Try removing the attribute altogether Commented Nov 22, 2014 at 12:03
  • still nothing, I get the null value Commented Nov 22, 2014 at 12:08
  • What does your routing rule look like and secondly, can you hit it from your browser? Commented Nov 22, 2014 at 12:30

1 Answer 1

1

Basically what you are expecting is for angular js $http.post to behave like jQuery.ajax(). This isn't the case because jquery.ajax transmits with contentType=x-www-form-urlencoded while angular always transmits with contentType=application/json. I would look at your API and check if you can transmit a request with content type json. There are add-on's to browsers that you can use to facilitate this. You can also work around this by ensuring Angular's $http.post behave like the jquery.ajax call by extending it. See the following link for details

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.