0

I am trying to pass values from angular to a web api.

Currently what I have for angular which does communicate with the webapi since I have it counting when something connects to it.

var data = new FormData();
data.append("x", "1");
data.append("y", "2");
$scope.http({ method: 'PUT', url: urltowebapi, data: data });

What I have on the webapi side is supposed to read the values but it always comes back as {}.

HttpContent req = Request.Content;
string str = req.ReadAsStringAsync().Result; 
3
  • If you're using Microsoft Web API, why are you reading content directly from the Request rather than using JSON serialization to pass the data to your controller? Commented Mar 4, 2014 at 20:03
  • Attempting to throw everything I could think of at the wall and see what would work. I've been staring at this all day and kind of gave up logic awhile ago. Commented Mar 4, 2014 at 20:05
  • The web api is going to come in through a controller. Could you post the code for it please? Commented Mar 5, 2014 at 1:16

1 Answer 1

1

Somehow I got it.

In angluar I am passing the data.

$scope.http({ method: 'PUT', url: controllerurl, data: { data: data } });

And in my controller I read it and deserialize the json. I think it was how I had the angular passing the data before but I don't know.

HttpContent req = Request.Content;
string str = req.ReadAsStringAsync().Result; 
var jObj = (JObject)JsonConvert.DeserializeObject(str);
string data= jObj["data"].ToString();
Sign up to request clarification or add additional context in comments.

Comments

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.