My question is quite similar to this but I couldn't figure out how can I keep things DRY. By using the answer of this post, I can authenticate check a particular AJAX call but my application has several AJAX calls and if I go by this approach then I will have to keep a check on every AJAX call success callback. Which is not quite cool.
Can you suggest a nice way to take care of this and keep things DRY?
Javascript
function GetStuff(x) {
if (!x.value) return;
$.post('/website/GetStuff', { val: x.value, text: x.text },
function (data) {
$("#ddlStuff").empty().append($("<option>").attr("value", "0").text("Choose...")).removeAttr("disabled");
$.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("#ddNewStuff")); });
});
}
C#
[HttpPost]
public JsonResult GetSutff(long val, string text)
{
List<string> data = new MyClass().GetDataFromDB(val, text);
return Json(data);
}
Since the controller where GetStuff action is defined has Authorise tag on top of it. Therefore, all calls made here will have to be authenticated. Now, if the session expires, then this action GetStuff returns home page's html (home page having return url - pretty standard stuff) and that causes an issue. Like this there are other view pages as well whose ajax post actions are authenticated and they also fail on client side when the session fails and html of home page gets returned.
I want the user to be redirected a session expired kinda page and want to know a nice and cool way of doing that. I hope I have made things clear. If not, let me know.
Thanks