6

In my Angularjs service I have this code:

$http.post("/EditWorkout/GetId", data).error(function (responseData) {
            console.log("Error !" + responseData);
        });

And I have this method in my ASP.net controller:

       [System.Web.Http.HttpPost]
        public JsonResult GetId(string routineId)
        {
            try
            {
                string x = routineId;
                return Json(new {success = true});
            }
            catch (Exception ex)
            {

                return Json(new { success = false, errorMessage = ex.Message });
            }

        }

I've put a break point on return Json(new {success = true}); and it gets fired, but my routineId is for some reason null, and data which I send using angular's $http.post isn't.

Why is this happening ?

3
  • What's inside the data variable? And what's the Content-Type request header being sent? Look at the development console of your Google Chrome browser to see the exact request being made. Commented Oct 8, 2013 at 9:07
  • Yes, it is a variable, I've even tried using "sometext" instead of data, and Content-Type is application/json;charset=utf-8 Commented Oct 8, 2013 at 9:12
  • And Status Code is HTTP/1.1 200 OK Commented Oct 8, 2013 at 9:15

1 Answer 1

8

try this:

$http.post("/EditWorkout/GetId", { routineId : data}).error(function (responseData) {
            console.log("Error !" + responseData);
        });
Sign up to request clarification or add additional context in comments.

2 Comments

and... why does this work? is it just because routineId is the same name in the controller and json object?
@chiliYago that is correct. Your JSON will be parsed as the C# object that is a parameter(s) on your API controller so they must match or will get an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.