GET请求的重定向,浏览器,但没有成功启动 [英] GET request for redirect initiated by browser but not successful

查看:264
本文介绍了GET请求的重定向,浏览器,但没有成功启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图重定向用户到一个URL,它的工作原理与GET请求,但不与回送请求。

通过Firebug的网络窗口,我可以看到回传请求后收到的浏览器重定向响应(也应该引起重定向)完成。浏览器似乎启动对重定向URL一个GET请求,但实际上并没有成功重定向。它仍然有在同一页上。

我使用JSF服务器端。启动的GET请求未由服务器接收的。然而,通过对服务器的需求,浏览器启动。我想问题出唯一客户端

任何人都可以请解释如何使重定向工作顺利?让我知道柜面我应该提供更多的信息。

修改

有关重定向请求头:

  GET /Px10Application/welcome.xhtml HTTP / 1.1
主持人:本地主机:8080
用户代理:Mozilla的/ 5.0(Windows NT的6.2; RV:20.0)的Gecko / 20100101 Firefox的/ 20.0
接受:应用/ XML,文本/ XML,* / *; Q = 0.01
接受语言:EN-US,EN; Q = 0.5
接受编码:gzip,紧缩
DNT:1
引用站点:HTTP://本地主机:8080 / Px10Application / channelPages.xhtml的channelID = -3412&放大器;类型=集团
的X请求 - 由于:XMLHtt prequest
面请求:部分/ AJAX
内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8
饼干:HB 8 = WQ :: db6a8873-f1dc-4dcc-a784-4514ee9ef83b; JSESSIONID = d40337b14ad665f4ec02f102bb41; oam.Flash.RENDERMAP.TOKEN = -1258fu7hp9
连接:保持活动
杂注:无缓存
缓存控制:无缓存
 

有关重定向响应头:

  HTTP / 1.1 200 OK
的X已启动方式:的Servlet / JSP 3.0 / 2.2(GlassFish应用服务器版3.1的Java / Sun微系统公司/ 1.6)
服务器:GlassFish应用服务器版3.1
设置Cookie:oam.Flash.RENDERMAP.TOKEN = -1258fu7hp8;路径= / Px10Application
杂注:无缓存
缓存控制:无缓存
到期日:-1
内容类型:text / xml的,字符集= UTF-8
内容长度:262
日期:星期三,2013 17点18分56秒格林尼治标准​​时间5月22日
 

解决方案

X-要求,通过:XMLHtt prequest 面请求:部分/ AJAX

您正在这样尝试使用派在JSF Ajax请求的重定向纯香草的Servlet API的 HttpServletResponse的#的sendRedirect()。这是不对的。该 XMLHtt prequest 不会把一个302的响应作为新的 window.location的,而只是作为一个新的Ajax请求。然而,当你返回一个完整的单纯功能的HTML网页与HTML部分要更新的指令Ajax响应,而不是predefined XML文档,JSF的Ajax引擎没有任何线索,如何处理重定向Ajax请求的响应。你结束了一个JS错误(你没看到它的JS控制台?),并没有任何形式的视觉反馈,如果你没有在 jsf.ajax.onError()配置的处理程序。

为了指示JSF Ajax引擎来改变 window.location的,你需要返回一个特殊的XML响应。如果你已经使用<一个href="http://docs.oracle.com/javaee/6/api/javax/faces/context/ExternalContext.html#redirect%28java.lang.String%29"相对=nofollow> 的ExternalContext#重定向() 相反,那么就发生了完全透明的。

  externalContext.redirect(的redirectUrl);
 

不过,如果你不JSF上下文中,如:在Servlet过滤器左右,因而不具备 FacesContext中在手,那么你应该可以手动创建并返回了特殊的XML响应。

 如果(分/ AJAX.equals(request.getHeader(面孔请求))){
    response.setContentType(为text / xml);
    response.getWriter()
        .append(&所述;?xml的版本= \1.0 \编码= \UTF-8 \&GT;?)
        .printf(&LT;部分响应&GT;&LT;重定向URL = \%s \的&GT;&LT; /重定向&GT;&LT; /部分响应&gt;中,的redirectUrl);
} 其他 {
    response.sendRedirect(的redirectUrl);
}
 

如果你碰巧使用JSF工具库 OmniFaces ,那么你也可以使用<一个href="http://omnifaces.org/docs/javadoc/2.1/org/omnifaces/util/Servlets.html#facesRedirect-javax.servlet.http.HttpServletRequest-javax.servlet.http.HttpServletResponse-java.lang.String-java.lang.String...-"相对=nofollow> 的Servlet#facesRedirect() 这项工作:

  Servlets.facesRedirect(请求,响应的redirectUrl);
 

参见:

  • <一个href="http://stackoverflow.com/questions/14580267/jsf-redirect-isnt-working-when-button-submitted/14582031#14582031">Authorization重定向会话过期不上提交JSF表单的工作,页面保持不变
  • <一个href="http://stackoverflow.com/questions/13366936/jsf-filter-not-redirecting-after-initial-redirect/13369719#13369719">JSF过滤器不是重定向初始重定向

While trying to redirect user to a URL, it works with GET requests but not with postback requests.

Through firebug's Network window, I can see the redirect response received by browser after the postback request (that should cause redirect) completes. The browser seemingly initiates a GET request for the redirect URL but doesn't actually successfully redirect. It remains there on the same page.

 I use JSF server side. The initiated GET request is not received at all by the server. However initiated by the browser on server's demand. I guess problem is somewhere client side only 

Can anyone please explain how to make redirect work successfully ? Let me know incase I should provide any more information.

Edit:

Request header for redirect:

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

Response header for redirect:

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

You're thus attempting to send a redirect on a JSF ajax request using "plain vanilla" Servlet API's HttpServletResponse#sendRedirect(). This is not right. The XMLHttpRequest does not treat a 302 response as a new window.location, but just as a new ajax request. However as you're returning a complete plain vanilla HTML page as ajax response instead of a predefined XML document with instructions which HTML parts to update, the JSF ajax engine has no clues what to do with the response of the redirected ajax request. You end up with a JS error (didn't you see it in the JS console?) and no form of visual feedback if you don't have the jsf.ajax.onError() handler configured.

In order to instruct the JSF ajax engine to change the window.location, you need to return a special XML response. If you have used ExternalContext#redirect() instead, then it would have taken place fully transparently.

externalContext.redirect(redirectURL);

However, if you're not inside JSF context, e.g. in a servlet filter or so, and thus don't have the FacesContext at hands, then you should be manually creating and returning the special XML response.

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);
}

If you happen to use JSF utility library OmniFaces, then you can also use Servlets#facesRedirect() for the job:

Servlets.facesRedirect(request, response, redirectURL);

See also:

这篇关于GET请求的重定向,浏览器,但没有成功启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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