如何管理一个jQuery Ajax调用后的重定向请求 [英] How to manage a redirect request after a jQuery Ajax call

查看:409
本文介绍了如何管理一个jQuery Ajax调用后的重定向请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 $。员额()使用Ajax,然后使用生成的HTML片段替换 DIV 在用户的当前页面元素。但是,如果会话超时,服务器发送一个重定向指令用户发送到登录页面。在这种情况下,jQuery是用在登录页面中的内容替换 DIV 元素,迫使用户的眼睛见证确实难得一见的场景。

I'm using $.post() to call a servlet using Ajax and then using the resulting HTML fragment to replace a div element in the user's current page. However, if the session times out, the server sends a redirect directive to send the user to the login page. In this case, jQuery is replacing the div element with the contents of the login page, forcing the user's eyes to witness a rare scene indeed.

我如何从一个Ajax调用使用jQuery 1.2.6管理重定向指令?

How can I manage a redirect directive from an Ajax call with jQuery 1.2.6?

推荐答案

这是最终实现的解决方案是使用一个包装Ajax调用回调函数,并在此包装检查一个特定元素的存在返回的HTML块。如果被发现的元素,则包装器所执行的重定向。如果不是,则包装器转发该呼叫到实际回调函数。

The solution that was eventually implemented was to use a wrapper for the callback function of the Ajax call and in this wrapper check for the existence of a specific element on the returned HTML chunk. If the element was found then the wrapper executed a redirection. If not, the wrapper forwarded the call to the actual callback function.

例如,我们的包装函数是这样的:

For example, our wrapper function was something like:

function cbWrapper(data, funct){
    if($("#myForm", data).size() > 0)
        top.location.href="login.htm";//redirection
    else
        funct(data);
}

然后,使Ajax调用,我们使用的是这样的时候:

Then, when making the Ajax call we used something like:

$.post("myAjaxHandler", 
       {
        param1: foo,
        param2: bar
       },
       function(data){
           cbWrapper(data, myActualCB);
       }, 
       "html"
);

这为我们工作,因为所有的Ajax调用始终是我们用来替换一张页面的DIV元素中返回的HTML。此外,我们只需要重定向到登录页面。

This worked for us because all Ajax calls always returned HTML inside a DIV element that we use to replace a piece of the page. Also, we only needed to redirect to the login page.

这篇关于如何管理一个jQuery Ajax调用后的重定向请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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