0

I am trying to post data and return some content posting to a .net web api using angularjs

here is my web api

[HttpPost]
    public HttpResponseMessage Post()
    {
        return new HttpResponseMessage()
        {
            Content = new StringContent(
                "<strong>test</strong>",
                Encoding.UTF8,
                "text/html"
            )
        };
    }

and here is my post from my client

$http({
                url: 'my happy url',
                method: "POST",
                data: objData,
                headers: {
                    'Content-Type': 'text/html'
                }
            }).success(function (data, status, headers, config) {
                console.log(headers());
            }).error(function (data, status, headers, config) {
                console.log("error");
            });

my problem is I do not get any data returned. I am sure I am missing something simple. Thanks for your help.

in the above code the data parameter is empty and I would expect to find the string "test" in the data parameter.

6
  • If you print it to console what does it show? undefined? Commented Jan 27, 2016 at 18:24
  • so, .success is being called? You're only logging headers, what about the data parameter? Commented Jan 27, 2016 at 18:29
  • Try to console.log() all the parameters in the success function. Commented Jan 27, 2016 at 18:31
  • I was looking for results in the data parameter which is empty. I left the headers in as it was the last thing I was using as I was debugging. Commented Jan 27, 2016 at 19:56
  • Have you tried to access the WebAPI data via other clients like PostMan or Fiddler? Did it worked there? Commented Jan 27, 2016 at 20:00

1 Answer 1

1

Instead of HttpResponseMessage, can you try to use IHttpActionResult? Sample code as below.

public IHttpActionResult PostSample()
{
    return Content(HttpStatusCode.OK, "Test String");
}
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.