Different behavior in GET vs. POST Ajax request

删除回忆录丶 提交于 2019-12-11 08:45:41

问题


We have an MVC app that uses controllers for AJAX endpoints, and FormsAuth for authentication.

I've run into an interesting scenario where a GET request will behave differently than a POST request (both for an unauthorized user).

In this particular case, our custom ControllerFactory runs the following code trying to access this controller:

FormsAuthentication.SignOut();
requestContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl);
throw new UnauthorizedAccessException();

(I realize that redirecting inside an AJAX request makes no sense, but bear with me).

When I do a GET request (AJAX) to this controller, the client receives a 401 - Unauthorized exception, which I can trap on the client side and redirect the user to the login page.

When I do a POST request (AJAX) to this controller, I'm getting a 302, and my request got redirected to my login page.

Why do the GET and POST requests act differently?


回答1:


So I took Darin's advice and did some refactoring, and I no longer run into this problem. :) I discovered the root of my problem, which was that we had a attribute for MVC error handling that did not have the IExceptionFilter attribute, so some stuff was happening in non-determinate orders. Thanks for the helpful kick in the butt. ;)



来源:https://stackoverflow.com/questions/6739102/different-behavior-in-get-vs-post-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!