不同的行为GET与POST Ajax请求 [英] Different behavior in GET vs. POST Ajax request
问题描述
我们有一个MVC应用程序,使用控制器AJAX端点和FormsAuth进行身份验证。
We have an MVC app that uses controllers for AJAX endpoints, and FormsAuth for authentication.
我碰到了一个有趣的场景,其中一个GET请求的行为不同于POST请求(包括未经授权的用户)。
I've run into an interesting scenario where a GET request will behave differently than a POST request (both for an unauthorized user).
在这种特殊情况下,我们的自定义的ControllerFactory运行以下code试图访问该控制器:
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();
(我认识到,重定向一个AJAX请求里面是没有意义的,但和我一起承担)。
(I realize that redirecting inside an AJAX request makes no sense, but bear with me).
当我做一个GET请求(AJAX)这个控制器,客户端会收到一个401 - 未经授权例外,我可以捕获在客户端和用户重定向到登录页面
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.
当我做一个POST请求(AJAX)这个控制器,我得到一个302,和我的要求得到重定向到我的登录页面。
When I do a POST request (AJAX) to this controller, I'm getting a 302, and my request got redirected to my login page.
为什么GET和POST请求采取不同?
Why do the GET and POST requests act differently?
推荐答案
所以我把达林的意见,并做了一些重构,我不再遇到这个问题。 :)我发现我的问题,这是我们有一个属性MVC错误处理那些没有个IExceptionFilter的属性的根,所以有些东西是发生在非确定的订单。感谢您的帮助的踢在屁股上。 ;)
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. ;)
这篇关于不同的行为GET与POST Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!