有一个页面(url),我通过XMLHttpRequest请求它,但是我没有得到请求的URL的响应,它正在将请求发送到另一个页面,
请求---> page.php
从> directedpage.php获取响应
问题是如何获得响应网址?(示例中为directedpage.php)
以前,servlet使用response.sendRedirect("pages/my_page.jsp?foo=bar");没有问题.可以在重定向到的后续页面中检索会话属性.
目前,我正在改变发送请求的方式.最初,Javascript使用myForm.submit();,但我现在已将其更改为jQuery.ajax("my_page.jsp?foo=bar", {...});.然后,servlet在JSON响应中包含一个URL,而不是response.sendRedirect()在success函数中,我window.location.replace(url);用来导航到新页面.但是,无法在后续页面中获取已保存的会话属性.
我通过插入<%= session.getId() %>JSP页面来比较会话ID .他们是一样的.这里的问题是重定向到的页面中的session.getAttribute("myAttribute_1")返回null.
我不确定这是否重要,但我实际上是在使用多个JSP页面执行此任务:
A.jsp --- (redirect) ---> B.jsp --- (redirect) ---> C.jsp
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如何获取保存的会话属性C.jsp?
下面是我用来保存会话属性的代码片段.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
response.setContentType("application/json");
CustomObject customObject = new CustomObject();
// ...
session.setAttribute("myAttribute_1", customObject);
PrintWriter writer = response.getWriter();
JsonObject json = new JsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
json.addProperty("url", "next_page.jsp?foo=bar"); …Run Code Online (Sandbox Code Playgroud) 我正在制作这样的Ajax请求:
$(".box01 .selproduct").live("click", function(e) {
var color = $(this).parent('.box01').find('.color').val();
var size = $(this).parent('.box01').find('.size').val();
var pid=$(this).parent('.box01').find('.hdinput').val();
var pathname = window.location.pathname;
var data = { submit: "selected",size:size,color:color,pid: pid};
$.ajax({
type: "POST",
url: pathname,
data: data,
success: function(data) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
},
complete: function(data) {
}
});
return false;
});
Run Code Online (Sandbox Code Playgroud)
在服务器端,我做了一些像这样的代码:
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["pid"]))
{
var path = HttpContext.Current.Request.Url.AbsolutePath;
HttpContext.Current.Response.Redirect(path);
}
Run Code Online (Sandbox Code Playgroud)
Ajax POST工作正常.我可以在Mozilla的Web Developer Tools中看到,但是页面没有像我想象的那样被重定向到其他页面.谁能告诉我我做错了什么?
或者是不可能Response.Redirect通过Ajax 调用?