Ajax:无法重定向到 AEM SLING servlet 内的内容页面
Ajax : Not able to redirect to a content page inside AEM SLING servlet
对 servletA 的 ajax 调用。 servletA 需要重定向或转发到另一个内容页面。可能吗。?
我在 Ajax done 函数中看到重定向的响应,而不是转到重定向的内容页面。可能是我错过了一些重要但不知道的东西。我试图重定向到 aem servlet 中的另一个内容页面。它在网络选项卡中返回 200 OK 响应,但永远不会转到指定的重定向页面。
我的同事说 Ajax 调用重定向是不可能的,因为它是单独的线程请求,是吗?如果我说回应,我是在假设之下。发送重定向();将发出新请求并在浏览器上加载响应 window。
@Component(
immediate = true,
service = Servlet.class,
property = {
"sling.servlet.paths=/bin/test/redirect"
})
public class TestRedirectServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = 3591693830449607948L;
@Override
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response) {
PrintWriter writer = null;
try {
writer = response.getWriter();
// final RequestDispatcherOptions options = new RequestDispatcherOptions();
// final SlingHttpServletRequest syntheticRequest = new SyntheticSlingHttpServletGetRequest(request);
// request.getRequestDispatcher("/content/<project-name>/en_US.html", options).forward(syntheticRequest, response);
// return;
response.sendRedirect("/content/test-site/sample.html");
} catch(Exception e) {
} finally {
if (writer != null) {
writer.print("done");
writer.flush();
writer.close();
}
}
}
}
通过使用 response.sendRedirect("/content/test-site/sample.html");
,您实质上是向您发回 HTTP 301 或 302 重定向给您 AJAX 请求。如果此请求是通过浏览器导航发起的,window 将重定向,但由于重定向是由 AJAX 发起的,响应将不会包含成功的 HTTP 200 状态代码,AJAX 将认为这是一个失败,可能会触发您的 error
函数回调。
更多关于 HTTP Status Codes。
如前所述,您可以使用 servlet 的打印编写器 return 一个 JSON 响应而不是重定向,并使用 JavaScript 客户端执行重定向。用 JSON 喜欢:
{
"redirect": true,
"location": "/content/test-site/sample.html"
}
您可以使用这样的 AJAX 调用:
$.ajax({
url: "/bin/test/redirect",
success: function(result){
if(result.redirect){
window.location = result.location;
}
},
error: function(result){
alert("Redirection Failure");
}
});
更多关于 jQuery AJAX。
对 servletA 的 ajax 调用。 servletA 需要重定向或转发到另一个内容页面。可能吗。? 我在 Ajax done 函数中看到重定向的响应,而不是转到重定向的内容页面。可能是我错过了一些重要但不知道的东西。我试图重定向到 aem servlet 中的另一个内容页面。它在网络选项卡中返回 200 OK 响应,但永远不会转到指定的重定向页面。
我的同事说 Ajax 调用重定向是不可能的,因为它是单独的线程请求,是吗?如果我说回应,我是在假设之下。发送重定向();将发出新请求并在浏览器上加载响应 window。
@Component(
immediate = true,
service = Servlet.class,
property = {
"sling.servlet.paths=/bin/test/redirect"
})
public class TestRedirectServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = 3591693830449607948L;
@Override
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response) {
PrintWriter writer = null;
try {
writer = response.getWriter();
// final RequestDispatcherOptions options = new RequestDispatcherOptions();
// final SlingHttpServletRequest syntheticRequest = new SyntheticSlingHttpServletGetRequest(request);
// request.getRequestDispatcher("/content/<project-name>/en_US.html", options).forward(syntheticRequest, response);
// return;
response.sendRedirect("/content/test-site/sample.html");
} catch(Exception e) {
} finally {
if (writer != null) {
writer.print("done");
writer.flush();
writer.close();
}
}
}
}
通过使用 response.sendRedirect("/content/test-site/sample.html");
,您实质上是向您发回 HTTP 301 或 302 重定向给您 AJAX 请求。如果此请求是通过浏览器导航发起的,window 将重定向,但由于重定向是由 AJAX 发起的,响应将不会包含成功的 HTTP 200 状态代码,AJAX 将认为这是一个失败,可能会触发您的 error
函数回调。
更多关于 HTTP Status Codes。
如前所述,您可以使用 servlet 的打印编写器 return 一个 JSON 响应而不是重定向,并使用 JavaScript 客户端执行重定向。用 JSON 喜欢:
{
"redirect": true,
"location": "/content/test-site/sample.html"
}
您可以使用这样的 AJAX 调用:
$.ajax({
url: "/bin/test/redirect",
success: function(result){
if(result.redirect){
window.location = result.location;
}
},
error: function(result){
alert("Redirection Failure");
}
});
更多关于 jQuery AJAX。