初学ajax,不明白为什么我在浏览器调试的过程中readystate一直没有变化过,一直都是1.
这里是前端代码:
function getMoreContent(){
var xmlHttp;
var content=document.getElementById("keyword");
if(content.value=="")
{
return;
}
xmlHttp=createXMLHttp();
var url="search?keyword="+escape(content.value);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.send();
}
function createXMLHttp(){
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
if(!xmlHttp){
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")
}
}
return xmlHttp;
}
function callback(){
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
//交互成功获得数据
var result=xmlHttp.responseText;
//解析获得的数据
var json=eval("("+result+")");
//获得数据后展示到输入框的下面
alert(json);
}
}
}
function clearContent(){
var contentTableBody=document.getElementById("content_table_body");
var size=contentTableBody.childNodes.length;
for(var i=size-1;i>=0;i--)
{
contentTableBody.removeChild(contentTableBody.childNodes[i]);
}
}
这里是后端Servlet的代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("123");
String keyword=request.getParameter("keyword");
System.out.println(keyword);
List listData=getData(keyword);
//返回JSON 测试输出
System.out.println("11");
response.getWriter().write(JSONArray.fromObject(listData).toString());
请问问题出在了哪里呢?