我正在尝试从 jsp 页面进行 ajax 调用,如下所示,
<head>
<script>
function loadXMLDoc() {
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", "ajax_info.txt", true);
xmlhttp.send();
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
</script>
</head>
<body>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
这是第一次调用 servlet 过滤器,但在 ajax 调用期间我看不到 doFilter 被调用。
过滤器的 url 映射将所有传入请求映射到服务器作为 *。
为什么这里的 Ajax 调用没有调用 Servlet 过滤器?
最佳答案
<head>
<script>
function loadXMLDoc() {
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.onreadystatechange=function() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
xmlhttp.open("GET", "ajax_info.txt", true);
xmlhttp.send();
}
</script>
</head>
<body>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
使用onreadystatechange
函数。
关于java - Ajax XMLHttpRequest 是否调用 servlet doFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21312038/