1

I created an action in my controller that will be used from jQuery Ajax method to validate user logon details. How can configure my action to throw an error that is detected by $.Ajax error handler?

I'm using the following code to throw an error, but it is not detected by $.Ajax.

throw new HttpException(401, "message");

Here is my JS code:

$.ajax({
        url: $('#signin_submit').data('url'),
        data: params,
        type: "POST",
        success: function (aData, aTextStatus, aJqXHR) { Signin.success(aData, aTextStatus, aJqXHR); },
        error: function (aJqXHR, aTextStatus, anErrorThrown) { Signin.error(aJqXHR, aTextStatus, anErrorThrown); }
    })
juliano.net
  • 7,982
  • 13
  • 70
  • 164
  • This could be an idea: if the request is not returning data(or returns error), you may use window.location.href='redirect url'. This might help you: http://stackoverflow.com/questions/2927044/redirect-on-ajax-jquery-call –  Sep 13 '12 at 12:32
  • This won't work for me as I have to show an error message instead of redirect to an error page. – juliano.net Sep 13 '12 at 13:00

1 Answers1

0

If you have setup Forms Authentication in your application you can't just easily throw 401 errors from application. The framework will react to those status codes and behave differently.

There are ways like creating custom http modules you can overcome this issue as described in this post.

VJAI
  • 32,167
  • 23
  • 102
  • 164