重定向作为对Ajax请求的响应最终返回空

时间:2011-11-11 18:44:53

标签: ajax redirect primefaces

我们使用的是Primefaces 3M4,其中一个页面有一个p:dataTable,它使用ajax调用事件:

<p:ajax event="rowSelect" update=":newsForm:newsDlg" oncomplete="newsDlg.show();"/>

当会话超时时,页面被重定向到/login.xhtml,这适用于非ajax操作(菜单项等)但是当我在会话过期后在数据表中选择一行时页面不会更改到登录页面,在Firebug中我看到以下内容:

在Firebug的dashboard.xhtml标题部分

Response Headers
Server  Apache-Coyote/1.1
X-Powered-By    JSF/2.0
Location    http://localhost:8080/RetailerPortal/faces/login.xhtml
Content-Length  0
Date    Fri, 11 Nov 2011 18:32:42 GMT

Request Headers
Host    localhost:8080
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0
Accept  application/xml, text/xml, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Faces-Request   partial/ajax
X-Requested-With    XMLHttpRequest
Referer http://localhost:8080/RetailerPortal/faces/dashboard.xhtml
Content-Length  389
Cookie  csfcfc=_30Xsr; JSESSIONID=fg1bV1sZkzKIgNtkH0bz0N0f; JSESSIONID=C65BF4EED70299ABFE4B73614118295E

在dashboard.xhtml响应下

<?xml version='1.0' encoding='ISO-8859-1'?>
<partial-response><changes><update id="javax.faces.ViewState"><![CDATA[-3728406524126180805:2441995557020829808]]></update></changes></partial-response>

在dashbaoard.xhtml帖子

Parametersapplication/x-www-form-urlencoded
javax.faces.ViewState   7521050094575005695:7928145831130537413
javax.faces.behavior.even...    rowSelect
javax.faces.partial.ajax    true
javax.faces.partial.event   rowSelect
javax.faces.partial.execu...    newsForm:newsTable
javax.faces.partial.rende...    newsForm:newsDlg
javax.faces.source  newsForm:newsTable
newsForm    newsForm
newsForm:newsTable_instan...    3
newsForm:newsTable_select...    3
Source
newsForm=newsForm&newsForm%3AnewsTable_selection=3&javax.faces.ViewState=7521050094575005695%3A7928145831130537413&javax.faces.partial.ajax=true&javax.faces.source=newsForm:newsTable&javax.faces.partial.execute=newsForm:newsTable&javax.faces.partial.render=newsForm:newsDlg&javax.faces.behavior.event=rowSelect&javax.faces.partial.event=rowSelect&newsForm:newsTable_instantSelectedRowKey=3

在login.xhtml的标题下

Response Headers
Server  Apache-Coyote/1.1
X-Powered-By    JSF/2.0
Cache-Control   no-cache
Set-Cookie  JSESSIONID=MdhyizD+8IkuFvLZD+6jWlUz; Path=/RetailerPortal
Content-Type    text/xml;charset=UTF-8
Content-Length  196
Date    Fri, 11 Nov 2011 18:32:42 GMT
Request Headers
Host    localhost:8080
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0
Accept  application/xml, text/xml, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection  keep-alive
Referer http://localhost:8080/RetailerPortal/faces/dashboard.xhtml
X-Requested-With    XMLHttpRequest
Faces-Request   partial/ajax
Content-Type    application/x-www-form-urlencoded
Cookie  csfcfc=_30Xsr; JSESSIONID=fg1bV1sZkzKIgNtkH0bz0N0f; JSESSIONID=C65BF4EED70299ABFE4B73614118295E

在login.xhtml的XML部分

XML Parsing Error: no element found Location: moz-nullprincipal:{6ccf85cf-5c69-438c-a9bb-e66423a36a48} Line Number 1, Column 1:

^

回复代码

HttpServletResponse servResponse = (HttpServletResponse) response;                  
servResponse.sendRedirect("login.xhtml");
servResponse.setHeader("Access-Control-Allow-Origin", "*");

2 个答案:

答案 0 :(得分:0)

只是一个猜测 -

如果您尝试从ajax响应重定向,则无法执行301/302样式 - 您必须将消息发送回浏览器并通过javascript重定向浏览器。

可能非ajax正在使用,因为他们正在使用301 / 302s。

答案 1 :(得分:0)

我找到了这个问题的答案in this blog

使用doRedirect方法中博客帖子底部的相关代码。