[英]AJAX doesn't change my JSP dynamically?
我的这个AJAX代码有什么问题? 它应根据条件将按钮的状态更改为启用或禁用。
function loadXML(){
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
/* alert (xmlhttp.responseText); */
if(xmlhttp.responseText == true) {
document.getElementById('scan').disabled=false;
document.getElementById('secret').value = "true";
}
else if(xmlhttp.responseText == false){
document.getElementById('scan').disabled=true;
document.getElementById('secret').value = "false";
}
}
}
xmlhttp.open("GET", "ScanJobServlet", true);
xmlhttp.send();
}
setInterval("loadXML()", 5000 );
该函数每5秒钟执行一次,以检查Servlet响应中是否有更改。
这是我的Servlet:它具有一个事件侦听器,当我插入USB时,响应变为true;如果拔出USB,则响应变为false。
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(req, resp);
PrintWriter out = resp.getWriter();
RemovableStorageEventListener listener = new RemovableStorageEventListener()
{
public void inserted(Storage storage) {
status = true;
}
public void removed(Storage storage) {
status = false;
}
};
BundleContext bc = AppManager.getInstance().getBundleContext();
StorageManager sm = StorageManager.getInstance(KSFUtility.getInstance().getApplicationContext(bc));
sm.addListener(listener);
if (status==true)
{
out.print("true");
}
else
{
out.print("false");
}
}
在这段代码中
if (status==true)
{
out.print("true");
}
else
{
out.print("false");
}
您将返回字面量"true"
和"false"
。 尝试使用false
带引号的true
和false
。 在JavaScript中, "true"
和"false"
不同于true
和false
,因为双引号表示文字。 更新:
if (status==true)
{
out.print(true);
}
else
{
out.print(false);
}
在您的JavaScript代码中,尝试以下操作:
(xmlhttp.responseText == "true")
代替
(xmlhttp.responseText == true)
与(xmlhttp.responseText == false)相同 ,将其更改为(xmlhttp.responseText ==“ false”) (带引号)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.