I have the following web metod:
[WebMethod]
public static string SayHello()
{
if (HttpContext.Current.Session["UserData"] != null)
{
//do something
}
else
{
response.StatusCode = 401;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.StatusCode = 401;
throw new HttpException(401, "timeout");
}
}
and this is the call:
$.ajax({
url: 'Default.aspx/SayHello',
type: 'POST',
contentType: 'application/json; charset=utf-8',
async: false,
dataType: "json",
data: '{ }',
success: function(Result) {
//do something
},
error: function(xhr, textStatus, errorThrown) {
xhr.StatusCode = 500
}
});
I always get 500 (Internal Server Error) status code in $.ajax error var xhr.StatusCode Can I change it inside the WebMethod?
I want to change it to 401 (Unauthorized) and inside $.ajaxError redirect to the login page..