2

I have an ajax call that looks like this:

  $.ajax({
                url: '@Url.Action("SaveStudy")',
                type: "POST",
                contentType: 'application/json',
                data: JSON.stringify({
                    ....irrelevant data
                }),
                success: function (result) {

                      ....irrelevant data

                },
                error: function (result) {

                    //alert("I had an error");
                }
            });

The error section of that ajax call is reached when logic in my controller sends back a HttpStatusCodeResult. The controller code looks like this:

 if (Participants==null)
            {
                return new HttpStatusCodeResult(409, "No Players were selected!");
            }

Is there a way to get that error message 'No Players were selected' to display in an alert (or anything, really) in the error section of my AJAX call?

1 Answer 1

4

Take a look at the jQuery.ajax documentation. As of jQuery 1.5, you can use statuscode to get that information.

$.ajax({
  statusCode: {
    404: function() {
      alert("page not found");
    }
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

That works. However, what I was hoping for would be for me to programmatically build an error string on the controller side and have that passed up to the ajax call to then put into the alert. The error api has: error(jqXHR, textStatus, errorThrown), but textStatus just returns 'error' and errorThrown returns 'conflict'
@Scottingham - Ah, I see. Unfortunately, I'm not entirely sure how to accomplish that. Perhaps return JSON from your controller method (you are stringifying JSON data anyway - you could do it server-side). It would be a different format, of course, but your error handler can know how to pull out the correct data.

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.