Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have a jquery ajax call to my controller action. Now in my controller, it went catch. Now I need to invoke error function of my ajax call. If I throw error, this will work. Is there any other method to invoke error function.

Bye the way, my controller's action's return type is string/void.

share|improve this question
    
Can you provide code that shows what you've tried? – Rob Jun 12 '13 at 18:47
up vote 3 down vote accepted

Write this in your Action Throw new HttpException(); This will invoke error to your ajax function and handle it with this $.ajax method function error:function() {do something }

share|improve this answer

I've verified this now, so I'm posting it as an answer. You can throw an exception as @Timur mentioned, but that has overhead associated with it that you may not want/need.

In your action method, you can set Response.StatusCode to an error value (anything in the 300, 400, or 500 range), and it will cause jQuery to invoke the error handler instead of the success handler.

share|improve this answer
    
if I'd need to pass an actual error msg using that response you suggested to then display it, say, in an alert, how could I manage that process in the controller and, if you could, how do I catch it in the .js? – J.M. Echevarría Jul 16 '15 at 21:30
    
There's also a Response.StatusDescription that you can set to whatever string value you'd like. I believe this comes across as the final parameter in jQuery's error ajax handler. Also, to amend my original answer, the more correct MVC way of handling all of this is probably to return an HttpStatusCodeResult instead of setting values directly on the Response object. – Brian Ball Jul 17 '15 at 11:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.