5

I have a simple AngularJS $http block like so;

$http(req).then(function successCallback(response) {
    alert(response);
}, function errorCallback(response) {
    alert(response);
});

My issue is that when my ASP.NET Controller returns a HTTP error code, the JS errorCallback receives an object like;

{data: "", status: 304, config: Object, statusText: "Not Modified"}

No matter what I do, I can't seem to populate the data property in my callback.

If instead my controller returns a HTTP OK code, then the 'success' callback is invoked, and the return data is available. But not when it's an error... help!

The controller function is a WebAPI POST handler and looks like;

[System.Web.Http.HttpPost]
public async Task<HttpResponseMessage> Save([FromBody]object data)
{
    ...<snip>...

    return new HttpResponseMessage
    {
        StatusCode = HttpStatusCode.NotModified,
        Content = new JsonContent(JObject.FromObject(new
        {
            success = false,
            message = "User not authorised to perform this action."
        }))
    };
}

The same construct, but with;

StatusCode = HttpStatusCode.OK

Is successfully received in the success callback in the JS.

9
  • Is that just a typo in your code? data vs response? Commented May 1, 2016 at 10:43
  • @EvanTrimboli: yes, fixed. Commented May 1, 2016 at 10:45
  • 1
    @RJLohan 304 indicates that the information will not be re-transmitted. I'm not too well versed in that - however I would bet it's because your request has been cached. See here tools.ietf.org/html/rfc7232#section-4.1 Commented May 1, 2016 at 11:37
  • 2
    @Rob: wow, thanks for that. Turns out that the issue was the HTTP status code I chose. Somehow I managed to pick the only one that doesn't return data! Changing this error code produced the expected outcome, and now I better understand the situation. Just wish I'd posted this question before several days of head-on-desk action... Commented May 1, 2016 at 23:04
  • 1
    Also for future visitors: 403 (Forbidden) vs 401 (Unauthorized) status codes Commented May 1, 2016 at 23:19

1 Answer 1

2

So after reviewing comments on my question, it turns out that the problem was caused by my selection of Http status codes. I was returning

StatusCode = HttpStatusCode.NotModified,

This code has a special meaning, and it seems the 'data' blob is intentionally stripped.

Instead, if I use a code like

StatusCode = HttpStatusCode.Unauthorized,

The 'data' blob is populated as expected in the Angular callback.

This sample returns the expected error data;

[System.Web.Http.HttpPost]
public async Task<HttpResponseMessage> Save([FromBody]object data)
{
    ...<snip>...

    return new HttpResponseMessage
    {
        StatusCode = HttpStatusCode.Unauthorized,
        Content = new JsonContent(JObject.FromObject(new
        {
            success = false,
            message = "User not authorised to perform this action."
        }))
    };
}
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.