0

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.

1
  • 1
    It's error not failure. Commented Sep 16, 2016 at 9:24

2 Answers 2

2

Can you please replace failure with error:

error: function(response) {
  alert(response.d);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'd seen failure somewhere and began using that. I've updated the answer with the correct property I should've been using. statusText is the right property. Accept the edit and I'll mark the answer.
0

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

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

1 Comment

Can't remember why I was using failure, must've seen it somewhere on the internet somewhere and just copied it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.