Ogglas Ogglas - 1 month ago 7
C# Question

jQuery 1.11.3 Post receives Bad Request but does not go into fail

jQuery 1.11.3 Post receives Bad Request from HttpResponseMessage but does not go into fail. Message from server is the following string:


"StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:
, Headers: { }"


Should I not get an object back from HttpResponseMessage that says Bad Request? I'm using IIS Express.

Bad request

Back end:

[HttpPost]
public HttpResponseMessage DeleteOrderRow(int orderRowId)
{
var row = OrderRowData.LoadItem(orderRowId);
if (row == null)
{
AddAlert(AlertStyles.Danger, "Order row does not exist");
//Below is the example being returned
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
OrderRowData.Delete(orderRowId);
AddAlert(AlertStyles.Success, "Order row has been removed");
return new HttpResponseMessage(HttpStatusCode.OK);
}


jQuery:

$(document).on('click', '.delete-order-row', function (event) {
event.preventDefault();

var element = $(this);
var id = element.data('id');

if (id == null || id === -1) {
element.closest('tr').remove();
} else {
var url = element.attr('href');
$.post(url, { orderRowId: id })
.done(function (data) {
element.closest('tr').remove();
})
.fail(function (xhr, status, error) {
location.reload();
});
}
});


Update: Checked network connection after tip from @smoksnes. The server actually sends 200 OK even though
return new HttpResponseMessage(HttpStatusCode.BadRequest);
is sent from back end. Is this normal behavior from IIS Express?

Network tab

Update 2: Solved it client side with this code. Based on @smoksnes reply and that
IExceptionFilter
and
ExceptionFilterAttribute
is not present in my project I'm suspecting Umbraco. Has anyone else experienced this in Umbraco?

.done(function (data) {
if (data.indexOf("StatusCode: 400") !== -1) {
$(window).scrollTop(0);
location.reload();
} else {
element.closest('tr').remove();
}
})

Answer

Solved it client side with this code. Based on @smoksnes reply and that IExceptionFilter and ExceptionFilterAttribute is not present in my project I'm suspecting Umbraco.

.done(function (data) {
    if (data.indexOf("StatusCode: 400") !== -1) {
        $(window).scrollTop(0);
        location.reload();
    } else {
        element.closest('tr').remove();
    }
})
Comments