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.

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
  • Is that just a typo in your code? `data` vs `response`? – Evan Trimboli May 01 '16 at 10:43
  • @EvanTrimboli: yes, fixed. – RJ Lohan May 01 '16 at 10:45
  • What are you expecting to be in `data` when there's an error..? – Rob May 01 '16 at 10:49
  • @Rob; a JSON blob in the end, but I can't even get a simple string to come through. Only the HTTP status code and it's statusText – RJ Lohan May 01 '16 at 10:51
  • 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 https://tools.ietf.org/html/rfc7232#section-4.1 – Rob May 01 '16 at 11:37
  • Can you add a sample of the request you are sending – It-Z May 01 '16 at 17:23
  • 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... – RJ Lohan May 01 '16 at 23:04
  • @RJLohan No worries - glad it led you towards getting it working properly :). Would you mind posting your solution as an answer and marking it as accepted so that it can potentially help future visitors? Thanks! – Rob May 01 '16 at 23:17
  • 1
    Also for future visitors: [403 (Forbidden) vs 401 (Unauthorized) status codes](http://stackoverflow.com/questions/3297048/403-forbidden-vs-401-unauthorized-http-responses) – Rob May 01 '16 at 23:19

1 Answers1

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."
        }))
    };
}
RJ Lohan
  • 6,497
  • 3
  • 34
  • 54