html – GET请求重定向由浏览器启动但不成功

前端之家收集整理的这篇文章主要介绍了html – GET请求重定向由浏览器启动但不成功前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在尝试将用户重定向到URL时,它适用于GET请求,但不适用于回发请求.

通过firebug的网络窗口,我可以看到浏览器在回发请求(应该导致重定向)完成后收到的重定向响应.浏览器似乎启动了重定向URL的GET请求,但实际上并未成功重定向.它仍保留在同一页面上.

我使用JSF服务器端.服务器根本不接收发起的GET请求.但是由浏览器根据服务器的需求发起.我想问题只是客户端的问题

任何人都可以解释如何成功重定向工作?让我知道,我应该提供更多信息.

编辑:

请求标头重定向

GET /Px10Application/welcome.xhtml HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20100101 Firefox/20.0
Accept: application/xml,text/xml,*/*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip,deflate
DNT: 1
Referer: http://localhost:8080/Px10Application/channelPages.xhtml?channelId=-3412&type=Group
X-Requested-With: XMLHttpRequest
Faces-Request: partial/ajax
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: hb8=wq::db6a8873-f1dc-4dcc-a784-4514ee9ef83b; JSESSIONID=d40337b14ad665f4ec02f102bb41; oam.Flash.RENDERMAP.TOKEN=-1258fu7hp9
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

重定向的响应标头:

HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1 Java/Sun Microsystems Inc./1.6)
Server: GlassFish Server Open Source Edition 3.1
Set-Cookie: oam.Flash.RENDERMAP.TOKEN=-1258fu7hp8; Path=/Px10Application
Pragma: no-cache
Cache-Control: no-cache
Expires: -1
Content-Type: text/xml;charset=UTF-8
Content-Length: 262
Date: Wed,22 May 2013 17:18:56 GMT

解决方法

X-Requested-With: XMLHttpRequest
Faces-Request: partial/ajax

因此,您尝试使用“普通的vanilla”Servlet API的HttpServletResponse #sendRedirect()发送JSF ajax请求的重定向.这个不对. XMLHttpRequest不会将302响应视为新的window.location,而是将其视为新的ajax请求.但是,当您返回一个完整的普通HTML页面作为ajax响应而不是预定义的XML文档以及要更新HTML部分的指令时,JSF ajax引擎没有线索如何处理重定向的ajax请求的响应.你最终得到了一个JS错误(你没有在JS控制台中看到它吗?)如果你没有配置jsf.ajax.onError()处理程序,也没有任何形式的视觉反馈.

为了指示JSF ajax引擎更改window.location,您需要返回一个特殊的XML响应.如果您使用过ExternalContext#redirect(),那么它将完全透明地进行.

externalContext.redirect(redirectURL);

但是,如果您不在JSF上下文中,例如在servlet过滤器中,因此没有FacesContext,那么您应该手动创建并返回特殊的XML响应.

if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {
    response.setContentType("text/xml");
    response.getWriter()
        .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
        .printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>",redirectURL);
} else {
    response.sendRedirect(redirectURL);
}

如果您碰巧使用JSF实用程序库OmniFaces,那么您也可以使用Servlets#facesRedirect()作为作业:

Servlets.facesRedirect(request,response,redirectURL);

也可以看看:

> Authorization redirect on session expiration does not work on submitting a JSF form,page stays the same
> JSF Filter not redirecting After Initial Redirect

原文链接:https://www.f2er.com/html/231365.html

猜你在找的HTML相关文章