我使用AJAX异步填充列表。加载网页后,会触发一个填充列表框的AJAX请求。但是当我将此请求发送到服务器时,将返回一个非预期的网页(存在列表框),而不是列表数据(JSON)。
[成分]
haveList.jsp =有列表框。它包含<script src="loadData.js">
标记
loadData.js =它生成一个Ajax请求并将其发送到服务器。加载haveList.jsp
后,将调用loadData.js
。 (我只是检查请求使用alert从服务器获取正确的数据。)
returnPage.java =转发到haveList.jsp
的Servlet
returnJsonData.java =返回loadData.js
要求的json字符串的Servlet。
每个都显示如下。
[web.xml中]
<servlet>
<servlet-name>ReturnPage</servlet-name>
<servlet-class>pkg.ReturnPage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ReturnPage</servlet-name>
<url-pattern>/openHaveList.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DataManager</servlet-name>
<servlet-class>pkg.ReturnJsonData</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataManager</servlet-name>
<url-pattern>/requestListData.do</url-pattern>
</servlet-mapping>
[现象]
我发送了#34; openHaveList.do&#34;到服务器
Page&#34; haveList&#34;返回。
立即发生了一个AJAX请求以获取JSON数据。 (requestListData.do)(请参阅下面的loadData.js)
使用alert()
我检查了服务器的responseText
内容
我期待JSON数据,但haveList.jsp
的HTML就在那里。
[线索]
(1)当我从服务器获得非预期的html时,浏览器的地址栏显示http://localhost/openHaveList.do
(在其他页面中,我点击图像。然后openHaveList.do发生。)
(2)当我使用ZAP(Zed攻击代理)捕获请求/响应时,我看到 2 请求发生。
第一个:requestListData.do(响应:空)
第二个:openHaveList.do。 (响应:hasList.jsp的html)
因此,我怀疑AJAX请求是无意中重定向的。 (requestListData.do - &gt; openHaveList.do)
- 来源代码 -
的 [haveList.jsp]
没什么特别的。列表框
[loadData.js]
( function () {
var conn=null, addEvent=null, createXHR=null, loadNations = null, setAsyncLoad=null;
addEvent = (function(window){
if(window.addEventListener){
return function (element, eventName, callback, canCapture) {
element.addEventListener(eventName, callback, canCapture);
};
} else if(window.attachEvent){
return function (element, eventName, callback) {
element.attachEvent("on" + eventName, callback);
};
} else {
return function (element, eventName, callback) {
element["on" + eventName] = callback;
}
}
}(window));
createXHR = function () {
var xhrObj = null;
try{
xhrObj = new XMLHttpRequest();
} catch (exception){
try{
xhrObj = new ActiveXObject('Msxml2.XMLHTTP');
} catch (innerException){
xhrObj = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xhrObj;
};
loadAsync = function () {
var list = document.getElementById("list");
conn = createXHR();
conn.onreadystatechange = function () {
if(conn.readyState == 4){
if(conn.status == 200){
alert(conn.responseText); //--> Here it shows the html of haveList.jsp
}
}
};
conn.open('GET', 'requestListData.do');
conn.send();
};
addEvent(window, 'load', function () { loadAsync(); } , false);
} () );
[returnPage.java]
public class ReturnPage extends HttpServlet {
private static String page = "/haveList.jsp";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 1L);
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 1L);
dispatcher.forward(request, response);
}
}
[returnJsonData.java]
public class ReturnJsonData extends HttpServlet {
private String command = null;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
strUri = request.getRequestURI();
command = strUri.substring(request.getContextPath().length());
try {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 1L);
response.setContentType("application/json; charset=UTF-8" );
response.getWriter().print( loadListData(request, command) );
} catch (Throwable e) {
System.out.println("Failed to load list");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
strUri = request.getRequestURI();
command = strUri.substring(request.getContextPath().length());
try {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 1L);
response.setContentType("application/json; charset=UTF-8" );
response.getWriter().print( loadListData(request, command) );
} catch (Throwable e) {
System.out.println("Failed to load list");
}
}
private String loadListData(HttpServletRequest request, String command) throws Throwable
{
String codeList = null;
if("/requestListData.do".equals(command)){
codeList = codeDao.select(conn);
}
}
}
答案 0 :(得分:0)
我解决了!! ^^
<强> [原因] 强>
由于我的过滤器。
<强> [详细] 强>
实际上,haveList.jsp
是登录页面。我有一个过滤器,用于检查请求是否已登录。如果没有,该过滤器会将请求重定向到“hasList.jsp(登录页面)”。
当触发对服务器(requestListData.do)的ajax调用时,将重定向的请求过滤到登录页面,因为它没有登录。请参阅以下web.xml(之前)。
<filter>
<filter-name>Checker</filter-name>
<filter-class>filter.Checker</filter-class>
<init-param>
<param-name>escape</param-name>
<param-value>/Test/,/Test/gate.jsp,/Test/openHaveList.do</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Checker</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Checker</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<强> [解决] 强>
我将ajax调用注册到服务器作为例外。 (转义参数)在我的过滤器 servlet中,我不会将此类异常重定向到登录页面以编程方式。请参阅以下web.xml(之后)。
<filter>
<filter-name>Checker</filter-name>
<filter-class>filter.Checker</filter-class>
<init-param>
<param-name>escape</param-name>
<param-value>/Test/,/Test/gate.jsp,/Test/openHaveList.do,"/requestListData.do"</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Checker</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Checker</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
在此之后,我从服务器获取了JSON数据。 ^^