2015-02-05 142 views
0

我有一個請求(現在從我的本地主機本身發出)發送一個ajax請求dataType設置爲json。在jsp上,這個request被解析並且基於一些條件,我做了一個重定向到另一個頁面(駐留在服務器上)。瀏覽器不呈現重定向url

現在我明白重定向僅適用於存在於不同服務器上的資源。但是,即使使用.forward(),我也面臨着下面描述的相同問題。 我的客戶端(再次,該文件所在的服務器只爲現在)是:

Somefile.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 = "[email protected]"; 


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

請幫忙。

回答

1

您需要了解的ajax的意思,這是

網頁的更新部分,沒有你的情況

所以,重新加載整個頁面,如果你想渲染或重定向視圖(頁面)。只需創建一個http請求,而不是ajax調用。

局部視圖的櫃面,我勸你呈現爲,,

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

在阿賈克斯成功

+0

OK。但$('#output')。html(data)din't help too :(但現在讓我試試httprequest。感謝評論。 – svk 2015-02-06 15:44:52

0

所以,我覺得一個GET請求將在這種情況下就足夠了。這裏不需要POST,因爲我們不是基於post請求有效載荷創建/編輯/刪除服務器上的任何內容。 我發現這個職位 - How to redirect with post data

如果,萬一,我們一定需要一個POST只在這裏。對於我的用例,GET就足夠了。感謝所有的幫助。 OK。