MiscellaneousUser
MiscellaneousUser

Reputation: 3043

Failure not working ASP.NET MVC Ajax JQuery

The following is my ajax call which is working fine but its always success.

  $.ajax({
    type: "POST",
    url: '@(Request.RawUrl + "/postcomment")',
    dataType: "json",
    data: data,
    contentType: "application/json; charset=utf-8",
    success: function() {
      alert('success');
    },
    failure: function(response) {
      alert(response.d);
    }
  });

How can I cause the failure function to be activated. In the controller class, I'm doing

return new HttpStatusCodeResult(500, "Error message");

But in the failure message box doesn't appear, what do I need to do to get failure to work and display a message.

Upvotes: 0

Views: 96

Answers (2)

Can you please replace failure with error:

error: function(response) {
  alert(response.d);
}

Upvotes: 2

Martin Dawson
Martin Dawson

Reputation: 7656

Use the status code callback. Where did you get failure callback from? There is only an error callback.

$.ajax({
  statusCode: {
    500: function() {
      alert( "error" );
    }
  }
});

Upvotes: 0

Related Questions