2013-04-27 170 views
0

我的ajax一些如何看起来像这样: function getXMLHttpRequest(){ var xmlHttpReq = false;如果(window.XMLHttpRequest){xmlHttpReq = new XMLHttpRequest();如果(window.XMLHttpRequest) }否则,如果(window.ActiveXObject){ 尝试{想要在同一页上的导航页面

 xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); 
    } catch (exp1) { 
     try { 

     xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (exp2) { 
     xmlHttpReq = false; 
     } 
    } 
    } 
    return xmlHttpReq; 
} 

function makeRequest() { 
    var xmlHttpRequest = getXMLHttpRequest(); 
    xmlHttpRequest.onreadystatechange = getReadyStateHandler(xmlHttpRequest); 
    xmlHttpRequest.open("POST", "http://abc.com:8080/someservletServlet/", true); 
    xmlHttpRequest.setRequestHeader("Content-Type", 
     "application/x-www-form-urlencoded"); 
    xmlHttpRequest.send(null); 
} 


function getReadyStateHandler(xmlHttpRequest) { 

    return function() { 
    if (xmlHttpRequest.readyState == 4) { 
     if (xmlHttpRequest.status == 200) { 

      document.getElementById("xml").value = xmlHttpRequest.responseText; 
     } else { 
     alert("HTTP error " + xmlHttpRequest.status + ": " + xmlHttpRequest.statusText); 
     } 
    } 
    }; 
} but somehow the servlet is not bringing the response it should bring. can you help. what could be the possible error. 
+1

因为你需要使用Ajax,因为这将帮助你部分刷新页面内容 – Satya 2013-04-27 13:01:09

回答

0

Ajax是如果您提交请求到go.Because的方式,页面将会刷新,无论是其同一页面或不同。

如果你仍然想实现它使用不使用Ajax和页面的刷新是细跟你再看看你是否有这样的在你的servlet代码,这是造成其转发至其他网页

String nextJSP = "nextPage.jsp"; 
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP); 
dispatcher.forward(request,response); 
+0

是否有可能使用某种类型的java脚本.... AJAX是我以前从未工作过的东西......并且这些类型的代码(requsetDispather等)不在servlet中。我已验证.... – user2326831 2013-04-27 13:32:17

+0

Ajax非常简单。使用jQuery查看ajax hello world。 Jquery简化了它。 – 2013-04-27 13:34:57

0

如果您需要从某个其他URL加载一些数据,您需要发送一个AJAX请求(解释从哪里获取数据)并处理AJAX响应(解释如何处理获取的数据)。为了提供一个兼容浏览器的解决方案,你最好使用一些知名的JS库。例如,你可以使用jQuery在这种情况下,你的脚本可能看起来像:

$.ajax({ 
    url: "servletURL",//servlet URL to post data to 
    type: "POST",//request type, can be GET 
    cache: false,//do not cache returned data 
    data: {id : idOfData},//data to be sent to the server 
    dataType: "xml"//type of data returned 
}).done(function(data) { 
    //do something with XML data returned from server 
}); 

使用这种方法,你需要调用上面的JS代码,可能是包裹在一个JS的功能,在某些JS事件,即click,并处理响应数据,例如,通过将其内容附加到您的文本区域。

+0

嗨,我的ajax一些如何看起来像这样: – user2326831 2013-04-29 14:14:52