1

I am calling a controller method from the client using jQuery's ajax.

$.ajax({
    url: "/Services/VendorServices.asmx/SendVendorRequestNotifications",
    type: 'POST',
    contentType: "application/json",
    dataType: "json",
    data: JSON.stringify({ vendorModel: info }),
    timeout: requestTimeOut,
    success: function (data) {
        // Success
    },
    error: function (jqXHR, textStatus, errorThrown) {
        // Error: Would like to display meaningful message here!
    }
});

If my SendVendorRequestNotifications throws an exception, I'd like to display an error message on the client side. However, I can't seem to get a good message to display to the user.

textStatus:

error

errorThrown:

Internal Server Error

jqXHR.responseText:

{"Message":"One or more errors occurred.","StackTrace":" at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task'1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task'1.get_Result()","ExceptionType":"System.AggregateException"}

As you can see, none of this is useful data. I'd like to be able to gather a more meaningful message on the server and have it available to the error handler of the $.ajax call.

Is this possible?

13
  • Yes. Have a look at this. Can you show the controller code? May be helpful. stackoverflow.com/questions/4707755/… Commented Jun 23, 2017 at 23:12
  • @mjw: My actual controller has a lot going on. Not sure what would be of interest. The main thing is that I have a try...catch block, and my catch block logs the error and then rethrows the exception. Commented Jun 23, 2017 at 23:15
  • Can you show the stub? The catch block? Those are likely all that is needed to see what's being returned to the client. Commented Jun 23, 2017 at 23:16
  • @mjw: My catch block: ex = ex.GetInnerMostException(); if (log.IsErrorEnabled) log.ErrorFormat("{0} failed : {1}", nameof(MakeVendorRequests), ex.Message); throw; Commented Jun 23, 2017 at 23:18
  • @mjw: My method stub: [WebMethod(EnableSession = true)] public async Task<string> MakeVendorRequests(ViperVendorViewModel vendorModel) Commented Jun 23, 2017 at 23:19

1 Answer 1

1

To manually pass an HttpStatusCodeResult, try this:

return new HttpStatusCodeResult(HttpStatusCode.BadRequest, e.Response.ReasonPhrase);

Unfortunately, it looks like you're locked in to a return type that won't be processed as an AJAX error, so you'll need to consider changing the return type of your controller action to get the desired outcome. Good luck

Sign up to request clarification or add additional context in comments.

Comments

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.