且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ajax - 我需要在每个AJAX请求之前检查是否存在有效会话

更新时间:2022-01-12 19:13:07

我现在改变了我的概念,我正在使用请求标头'x-requested-with'检查服务器端的xmlHTTPRequest。

I changed my concept now, I'm checking for the xmlHTTPRequest in the server side using the request header 'x-requested-with'.

如果是是xmlHTTPRequest然后'x-requested-with'将具有值'XMLHttpRequest'。我正在使用的javascript库(EXTjs和jQuery)都正确设置了这个标题。

If it is a xmlHTTPRequest then 'x-requested-with' will have the value 'XMLHttpRequest'. Both the javascript libraries(EXTjs and jQuery) I'm using sets this header correctly.

这是我的服务器端代码

boolean isAjaxRequest = StringUtils.endsWithIgnoreCase(request.getHeader("x-requested-with"), "XMLHttpRequest")

编辑

如果给定的请求是ajax请求,则响应将是具有状态的json数据403它将包含一个名为timeout的键,值为true
ex:{timeout:true,....}

If the given request is a ajax request the response will be json data which will have status 403 and it will contain a key called timeout with value true ex: {timeout: true, ....}

然后我们将在$ .ajaxError()事件处理程序将处理错误。如果错误状态为403且超时值为true,那么我将重新加载页面。

Then we will handle this in the $.ajaxError() event handler will handle the error. If the error status is 403 and the timeout value is true then I'll reload the page.