无法在阿贾克斯和如何处理302重定向? [英] Cannot handle 302 redirect in ajax and why?

查看:135
本文介绍了无法在阿贾克斯和如何处理302重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用窗体身份验证后端服务器写在asp.net mvc的。当用户没有通过验证,服务器会自动发送一个302重定向到一个登录操作,并返回一个登录页面。

I have a back-end server written in asp.net mvc using Forms Authentication. When the user is not authenticated, the server will automatically send a 302 redirect to a Login action and return a Login page.

在客户端,我有一个项目列表。此列表只是访问身份验证的用户。在页面上,我有一个按钮,刷新使用Ajax($阿贾克斯功能的jQuery)名单。

On client side, I have a list of items. This list is only accessible to authenticated users. On the page, I have a button to Refresh the list using Ajax ($.ajax function of jQuery).

现在,我的问题是,当身份验证票证超时和用户点击刷新按钮:

Now, my problem is when the authentication ticket is timeout and the user clicks on the Refresh button:

  • 在我的函数发送一个Ajax请求得到刷新列表
  • 在服务器检测到身份验证票证是无效的,并发出302重定向。
  • 在浏览器的自动处理的302响应,迫使我的AJAX功能再派Ajax请求的登录操作和最终的结果是,状态200的HTML我的脚本困惑,因为该列表还有一个HTML,状态200。
  • My function sends an ajax request to get the refreshed list
  • The server detects that the authentication ticket is not valid and issues a 302 redirect.
  • The browser automatically handles that 302 response and forces my ajax function to send another ajax request to the Login action and the final result is an HTML with status 200. My script is confused because the list is also an HTML with status 200.

在身份验证票证超时,用户点击刷新按钮,我应该能够检测并显示一条消息,要求用户登录我要的是。

What I want is when the authentication ticket is timeout and the user clicks on the Refresh button, I should be able to detect that and display a message asking the user to Login.

我试图通过增加在登录操作的自定义页眉(IS_LOGIN)要解决这一点,并检查在我的Ajax响应。但它不是一个很好的解决方案。

I tried to workaround this by adding a custom header (IS_LOGIN) in the Login action and check that in my ajax response. But it is not a good solution.

所以,我的问题是:

  • 什么是解决这个问题的最好方法是什么?
  • 为什么浏览器不能让我们的脚本处理302回应?而只是自动迫使我们的Ajax创建另一个请求。这是浏览器或jQuery库有问题?任何原因? (安全性,...)

感谢您的任何答复。

推荐答案

您不能将呼叫转接当它是一个XHR但有一个 401未授权应对和处理这个问题在你的回调。我不知道ASP.NET,但我没有使用Spring Security类似的东西。

You shouldn't redirect the call when it's an XHR but respond with a 401 Unauthorized and handle this in your callbacks. I don't know ASP.NET but I did something similar with Spring Security.

继承人的理念:

  • 获得认证状态
  • 检查标题为 X-要求,通过:XMLHtt prequest
  • 当发现不符合认证响应401未授权
  • 在当前没有发现未经过身份验证的重定向。
  • Get the authenticated state
  • Check the headers for X-Requested-With: XMLHttpRequest
  • When found and not authenticated respond with 401 Unauthorized
  • When not found and not authenticated redirect.

的底线是,不同的处理,然后在某些情况下,其他的HTTP请求XHR调用需要。应该只重定向XHR如果相同的资源是在另一位置。

The bottom line is that XHR calls need to be handled differently then other HTTP requests in some cases. You should only redirect a XHR if the same resource is at another location.

由于浏览器照顾他们自动无法处理重定向与XHR回调。你只会拿回在重定向的位置。

You can't handle redirects with XHR callbacks because the browser takes care of them automatically. You will only get back what at the redirected location.

这篇关于无法在阿贾克斯和如何处理302重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆