AJAX HttpRequest 没有收到来自 servlet 的更新数据
AJAX HttpRequest doesn't recieve updated data from servlet
Servlet:
package world.hello;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import world.hello.MyMainClass;
public class TestServlet extends HttpServlet{
private static final int BYTES_DOWNLOAD = 1024;
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
response.setContentType("text/plain");
OutputStream os = response.getOutputStream();
os.write(("hello world"+Double.toString(Math.random())).getBytes());
os.flush();
os.close();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
doGet(request, response);
}
}
HTML:
<html>
<body>
<script>
function myAjax()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","RunQuery", false);
xmlhttp.send();
document.getElementById("myText").innerHTML=xmlhttp.responseText + " " + xmlhttp.readyState.toString() + " " + xmlhttp.status.toString() ;
document.getElementById("myText2").innerHTML=Math.random();
}
</script>
<button id = "myButton" onclick = "myAjax()">click me</button>
<div id = "myText"></div>
<div id = "myText2"></div>
</body>
</html>
如果我直接在 http://localhost:9070/test_web_project_1/RunQuery
访问 servlet
每次刷新时,都会显示不同的随机浮动。
当我运行 HTML at http://localhost:9070/test_web_project_1/myxjax.html
时,第二个float变了,第一个是固定的。
这是什么原因造成的,我该如何解决?
别管我之前说过的话...您的代码是同步的,因为您将 async 设置为 false。您的问题只是浏览器缓存。正在缓存您的 ajax 请求。您可以通过向请求添加带有 date/time 的参数来欺骗浏览器不加载缓存,例如:
var d = new Date();
xmlhttp.open("GET","RunQuery?ts="+d.getTime(), false);
这只会让浏览器将每个请求视为唯一;无需在服务器端对该参数执行任何操作。
或者,您可以在 Ajax 调用的 servlet 中添加 no-cache headers。您也可以同时执行这两项操作以格外小心。
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0);
Servlet:
package world.hello;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import world.hello.MyMainClass;
public class TestServlet extends HttpServlet{
private static final int BYTES_DOWNLOAD = 1024;
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
response.setContentType("text/plain");
OutputStream os = response.getOutputStream();
os.write(("hello world"+Double.toString(Math.random())).getBytes());
os.flush();
os.close();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
doGet(request, response);
}
}
HTML:
<html>
<body>
<script>
function myAjax()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","RunQuery", false);
xmlhttp.send();
document.getElementById("myText").innerHTML=xmlhttp.responseText + " " + xmlhttp.readyState.toString() + " " + xmlhttp.status.toString() ;
document.getElementById("myText2").innerHTML=Math.random();
}
</script>
<button id = "myButton" onclick = "myAjax()">click me</button>
<div id = "myText"></div>
<div id = "myText2"></div>
</body>
</html>
如果我直接在 http://localhost:9070/test_web_project_1/RunQuery
每次刷新时,都会显示不同的随机浮动。
当我运行 HTML at http://localhost:9070/test_web_project_1/myxjax.html
时,第二个float变了,第一个是固定的。
这是什么原因造成的,我该如何解决?
别管我之前说过的话...您的代码是同步的,因为您将 async 设置为 false。您的问题只是浏览器缓存。正在缓存您的 ajax 请求。您可以通过向请求添加带有 date/time 的参数来欺骗浏览器不加载缓存,例如:
var d = new Date();
xmlhttp.open("GET","RunQuery?ts="+d.getTime(), false);
这只会让浏览器将每个请求视为唯一;无需在服务器端对该参数执行任何操作。
或者,您可以在 Ajax 调用的 servlet 中添加 no-cache headers。您也可以同时执行这两项操作以格外小心。
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0);