2

I'm trying to handle a Status: HTTP/1.1 204 No Content error. I'm using .fail() to handle all other errors, but 204 is not handled by this function. While trying if (jqXHR.status == "204") {alert("error!");}, the alert is not thrown when a 204 is received.

$.ajax({
    url: url,
    method: 'GET',
    headers: {
        "Authorization": bearerToken
    },
}).then(function(response) {
    var obj = response;
    $("#imageid").css("border-color", "#ccc");
    $(".search-results").empty();
    for (var property in obj.entity.entries) {
        if (obj.entity.entries.hasOwnProperty(property)) {
            $(".search-results").append($("<li><a href='" + obj.entity.entries[property].uri + "' target='_blank'><div class='thumbnail'><img width='30' height='30' src='" + obj.entity.entries[property].uri + "' target='_blank'/></img><div class='caption'><p>" + obj.entity.entries[property].orientation + "</p></div></a></li>"));
        }
    }
    //$(".search-results").append("<div class='caption'>" + data.id + "</div><div class='thumbnail'><img width='40' height='40' src='" + data.thumbnailUrl + "'/></img>").css("float", "left");
}).fail(function(data, jqXHR) {
    if (jqXHR.status == "204") {
        $(".search-results").empty();
        $(".search-results").append("<p class='alert alert-danger'>Invalid ID</p>");
        $("#imageid").css("border-color", "red");
    }
    $(".search-results").empty();
    $(".search-results").append("<p class='alert alert-danger'>Invalid ID</p>");
    $("#imageid").css("border-color", "red");
});
Sushil
  • 2,837
  • 4
  • 21
  • 29
Matt
  • 1,239
  • 4
  • 24
  • 53
  • "If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view." – bhspencer May 12 '15 at 17:02

1 Answers1

4

2xx is considered success. Your success function should be getting called. Check the status code there.

jqXHR is the 3rd argument to the success callback.

Edit: here's code

then(function(data, responseText, jqXHR) {
  if(jqXHR.status == 204) alert('no content')
})
Farzher
  • 13,934
  • 21
  • 69
  • 100
  • How can I specify an alert to the user when `NO CONTENT` is returned in the `.then()` function? The id exists in the system, but there is no image, so I get a 204. – Matt May 12 '15 at 17:30
  • @Matt I told you how, and you still can't figure it out? o_O I edited my answer to add code for you to copy&paste. – Farzher May 12 '15 at 18:17
  • your edit didn't show up until after I commented...o_O – Matt May 12 '15 at 18:25
  • However, besides the syntax errors, it does not seem to be working. – Matt May 12 '15 at 18:31
  • The `data` callback breaks the function. `then(function (responseText, jqXHR) { if(jqXHR.status == 204){ alert("no content"); }` is not alerting when I get a 204. – Matt May 12 '15 at 18:44
  • I've applied it as seen here: http://jsfiddle.net/mattography/wkbzxw1e/, but it is having no affect on the response. – Matt May 14 '15 at 15:52