ajax
게시물 요청 (dataType
)을 json
으로 설정하는 요청 (현재는 내 로컬 호스트 자체에서 발생)이 있습니다. jsp에서는이 request
이 구문 분석되고 일부 조건에 따라 서버에있는 다른 페이지로 리디렉션됩니다.브라우저가 리디렉션 URL을 렌더링하지 않음
이제 리디렉션은 다른 서버에있는 리소스에만 해당하는 것으로 알고 있습니다. 그러나, 심지어 .forward(), 나는 아래에 설명 된 동일한 문제에 직면하고있다.
Somefile.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;
}
%>
:
<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>
그리고 서버 측
response
(Chrome에서 개발자 콘솔을 볼 때) 응답 (리디렉션 페이지)이 브라우저에 렌더링되지 않아 모든 컨트롤/레이블 등을 시각적으로 볼 수 있습니다.
나는이 문제도 시도했지만 (브라우저에서 응답을 시각적으로 볼 수 없음).
getServletContext().getRequestDispatcher("/xxx/yyy/zzz").forward(request,
response);
친절히 도움. 당신이하려고하는 경우
확인. 하지만 $ ('# output') .html (data) din't 역시 도움이됩니다. (그러나 이제는 httprequest를 시도해 보겠습니다. – svk