【问题标题】:Browser not rendering redirect url浏览器不呈现重定向网址
【发布时间】:2015-02-05 19:13:45
【问题描述】:

我有一个请求(现在来自我的本地主机本身)发送一个ajax 发布请求,其中dataType 设置为json。在 jsp 上,这个request 被解析并且基于某些条件,我重定向到另一个页面(驻留在服务器上)。

现在我了解到重定向仅适用于存在于不同服务器上的资源。但是,即使使用 .forward(),我也面临着下面描述的相同问题。 我的客户端(同样,这个文件现在只驻留在服务器上)是:

一些文件.html:

<html>
<head>
<script type="text/javascript"    
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
</script>
<script type="text/javascript">

    $(document).ready(function() {


var payload = new Object();

payload.userInfo = new Object();

payload.userInfo.firstName = "Sam";
payload.userInfo.lastName = "Peter";
payload.userInfo.emailAddress = "speter@gmail.com";


payload.accountInfo = new Object();
payload.accountInfo.accountId = "12321";
payload.accountInfo.agentName = "Harry";
payload.accountInfo.terminalId = "1322";

payload.locale = "en-US";
payload.loggedIn = "true";


var payloadjson = JSON.stringify(payload);


        $('#call').click(function ()
        {

            $.ajax({
                type: "post",
                url: "http://localhost:8280/Somepath/FormFiller.jsp", 
    //this is my servlet
        dataType: "json",
                success: function(data){      
                        $('#output').append(data);
                },
        data: {postData:payloadjson}
            });
        });

    });
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<input type="button" value="Call Servlet" name="Call Servlet" id="call"/>
<div id="output"></div>
</body>
</html>

在服务器端: FormFiller.jsp

<%@ page language="java" contentType="application/x-www-form-urlencoded; 
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ page import="org.json.simple.JSONObject"%>
<%@ page import="org.json.simple.JSONValue"%>


<%
//response.setHeader("Access-Control-Allow-Origin",     
request.getHeader("Origin"));

request.setCharacterEncoding("utf8");
response.setContentType("application/json");

JSONObject jsonObj = (JSONObject)    
JSONValue.parse(request.getParameter("postData"));

JSONObject userInfo = (JSONObject)   
JSONValue.parse(jsonObj.get("userInfo").toString());

JSONObject accountInfo = (JSONObject) 
JSONValue.parse(jsonObj.get("accountInfo").toString());
String locale = (String) jsonObj.get("locale");
String loggedIn = (String) jsonObj.get("loggedIn");

out.println(userInfo.get("firstName"));
out.println(userInfo.get("lastName"));
out.println(userInfo.get("emailAddress"));

out.println(accountInfo.get("accountId"));
out.println(accountInfo.get("agentName"));
out.println(accountInfo.get("terminalId"));

out.println(locale);
out.println(loggedIn);


if(loggedIn == "false") {

// Redirect to some site

}

else {

out.println(request.getContextPath());
// Redirect to some other site and fill the form
response.sendRedirect("/somepath/xxx/yyy/zzz"); // zzz is some html file
return;
}
%>

虽然我得到了response(当我在 chrome 上查看开发者控制台时),但响应(重定向页面)并未呈现在浏览器上,因此我可以直观地看到所有控件/标签等。

我也试过这个,但同样的问题(无法在浏览器上直观地看到响应)。

 getServletContext().getRequestDispatcher("/xxx/yyy/zzz").forward(request,  
    response);

请帮忙。

【问题讨论】:

    标签: jsp redirect


    【解决方案1】:

    你要明白ajax的意思,是为了

    更新网页的部分内容,而不重新加载整个页面

    因此,在您的情况下,如果您尝试渲染或重定向视图(页面)。只需发出 http 请求而不是 ajax 调用。

    如果是局部视图,我建议您将其渲染为,,

     $('#output').html(data);
    

    在你的 ajax 成功

    【讨论】:

    • 好的。但是 $('#output').html(data) 也无济于事 :( 但是现在让我试试 httprequest。感谢您的评论。
    【解决方案2】:

    所以,我认为在这种情况下一个 GET 请求就足够了。此处不需要 POST,因为我们不会根据发布请求有效负载在服务器上创建/编辑/删除任何内容。 我找到了这篇文章 - How to redirect with post data

    如果,以防万一,我们必须只在这里需要一个 POST。对于我的用例,GET 就足够了。感谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-15
      • 2013-03-07
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2013-09-24
      • 2016-01-02
      • 2014-10-26
      相关资源
      最近更新 更多