监视请求结果以检测会话超时的简单方法?

发布于 2024-09-10 04:25:29 字数 597 浏览 3 评论 0原文

我有一个 Ajax 应用程序,我想添加会话和登录。

目前,该应用程序使用 jQuery get 速记来发送请求,

that.ajaxRequest = $.get(query_string, function(data, textStatus, xhr) { 

  // do somthing with data

});

我在服务器上有一个系统,如果会话已过期,该系统会抛出错误消息。

在客户端,我知道我可以使用以下方法来处理这个问题:

that.ajaxRequest = $.get(query_string, function(data, textStatus, xhr) { 

  if (data == "E_TIMEOUT") {

       // redirect to login

  } else {

       // do something

  }

});

但是我不想为每个 $.get 编写相同的超时代码 - 整个应用程序中有很多这样的超时代码。

有没有办法监视请求的结果并在必要时采取行动,而无需重写所有 $.get 语句?

谢谢,

I've got an Ajax app I would like to add sessions and logins to.

Currently the app uses jQuery get shorthand to send a request

that.ajaxRequest = $.get(query_string, function(data, textStatus, xhr) { 

  // do somthing with data

});

I have a system on the server that throws an error message if the session has expired.

On the client I know I can deal with this using the following:

that.ajaxRequest = $.get(query_string, function(data, textStatus, xhr) { 

  if (data == "E_TIMEOUT") {

       // redirect to login

  } else {

       // do something

  }

});

However I would prefer not to have to write the same timeout code for every $.get - there are a bunch of them throughout the app.

Is there a way to monitor the result of a request and take action if necessary, without rewriting all of my $.get statements?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-09-17 04:25:29

您可以使用 $.ajaxSetup() 来实现此目的:

$.ajaxSetup({
  complete: function (xhr) {
    if (xhr.getResponseHeader('E_TIMEOUT') == "1") {
      //redirect to login
    }
  }
});

此方法是略有不同,它使用响应标头,您可以在每次请求时无需额外解析数据一次即可获得响应标头(这是可能的,尽管没有记录并且可能会中断),否则您必须在全球形势。

在您发送 E_TIMEOUT 响应的服务器端,只需更改它以添加标头 E_TIMEOUT=1 (或您想要的任何标头)。实际上,我只将此标头放在我的登录页面上,超时通过 302 重定向(XmlHttpRequest 透明地跟随),因此,如果用户超时和 AJAX 请求实际上在登录页面结束,我会执行 location.reload () 以在页面上反映这一点。如果您选择这条路线,这只是另一种选择,它看起来像这样:

$.ajaxSetup({
  complete: function (xhr) {
    if (xhr.getResponseHeader('LoginPage') == "1") {
      location.reload();
    }
  }
});

在不知道您正在使用什么服务器平台的情况下,我无法说出如何设置标头,但在每个常见平台上都非常容易。

You can use $.ajaxSetup() for this:

$.ajaxSetup({
  complete: function (xhr) {
    if (xhr.getResponseHeader('E_TIMEOUT') == "1") {
      //redirect to login
    }
  }
});

This approach is slightly different, it uses response headers which you can get without parsing data an extra time every request (that is possible, though not documented and subject to break), which you would have to do otherwise, in a global situation.

On the server-side where you're sending E_TIMEOUT for the response, just change it to add a header as well, E_TIMEOUT=1 (or whatever header you want). I actually put this header on my login page only, the timeouts redirect via a 302 (which XmlHttpRequest transparently follows), so if the user timeouts and the AJAX request actually ended up at the login page, I do a location.reload() to reflect this on the page. That's just another option if you go that route, it looks like this:

$.ajaxSetup({
  complete: function (xhr) {
    if (xhr.getResponseHeader('LoginPage') == "1") {
      location.reload();
    }
  }
});

Without knowing what server platform you're using I can't say how to set headers, but it's pretty easy on every common platform.

浪菊怪哟 2024-09-17 04:25:29

您应该使用 Firefox 并下载一个名为“FireBug”的程序。这将显示所有发出的请求以及从 AJAX 调用返回的响应。这是充满善良和正义的行动。网络开发工具。

You should use firefox and download a program called 'FireBug'. This will show you all the requests being sent out, and the response that comes back from the AJAX call. It is action packed with goodness & web dev tools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文