浏览器发起的重定向的GET请求但不成功 [英] GET request for redirect initiated by browser but not successful

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

问题描述

尝试将用户重定向到 URL 时,它适用于 GET 请求,但不适用于回发请求.

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

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

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.

我使用 JSF 服务器端.服务器根本没有收到发起的 GET 请求.但是由浏览器根据服务器的需求发起.我想问题只是在客户端的某个地方

 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.

重定向请求头:

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

因此,您尝试使用普通"Servlet API 的 HttpServletResponse#sendRedirect() 对 JSF ajax 请求发送重定向.这个不对.XMLHttpRequest 不会将 302 响应视为新的 window.location,而是将其视为新的 ajax 请求.然而,当您返回一个完整的普通 HTML 页面作为 ajax 响应而不是预定义的 XML 文档时,该文档包含要更新哪些 HTML 部分的说明,JSF ajax 引擎不知道如何处理重定向的 ajax 请求的响应.如果您没有配置 jsf.ajax.onError() 处理程序,您最终会遇到一个 JS 错误(您没有在 JS 控制台中看到它吗?)并且没有任何形式的视觉反馈.

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.

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

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

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

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

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

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

Servlets.facesRedirect(request, response, redirectURL);

另见:

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