如何“搜索”XMLHttpRequest的响应

时间:2011-01-23 18:11:27

标签: javascript html xmlhttprequest

我对以下代码有疑问

<html>
<head>
<script type="text/javascript">
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)
    {
    return xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","index.html",true);
xmlhttp.send();


}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

现在我想搜索xmlhttp.responseText(换句话说,调用函数 loadXMLDoc())来获取关键字,比如“testfile”,如果它存在多个例子“testfile_1”和“testfile_2”.....“testfile_n”然后“doSomething”

像这样

 <html>
<head>
<script type="text/javascript">
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)
    {
    return xmlhttp.responseText;
    }
  }



}
function searchADocument(wordToSearchFor){
xmlhttp.open("GET","index.html",true);
xmlhttp.send();
int numberOfTimesWordOccurs=0;
var thePageToSearchThrough [] = loadXMLDoc();
for (i=0; i<thePageToSearchThrough.length; i++){
if(thePageToSearchThrough[i]==wordToSearchFor)
 numberOfTimesWordOccurs++;
}
If  (numberOfTimesWordOccurs > 1) 
document.write("<a href="http://selnc05.go.se:8080/component_test/build/testfile_1">    testfile_1</a>"<a href="http://selnc05.go.se:8080/component_test/build/testfile_2">    testfile_2</a><a href="http://selnc05.go.se:8080/component_test/build/testfile_n">    testfile_n</a>

)

Else

window.location="http://selnc05.go.se:8080/component_test/build/testfile.html";

}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="searchADocument("testfile")">Change Content</button>

</body>
</html>

我不知道从哪里开始,因为我不知道xmlhttp.responseText是什么类型,我可以将它存储在一个数组中并使用for循环扫描等吗? 提前致谢。 =)

EDIT 我在这里做错了什么

<html>
<head>
<script type="text/javascript">
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)
    {

    return xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","index.html",true);
xmlhttp.send();
}

function searchADocument(){ //wordToSearchFor
var txt=loadXMLDoc();
if(txt.test('hello'))alert('responseText contains "hello"');
else{
    document.getElementById("myDiv").innerHTML ="helloaj";
}

}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="searchADocument()">Change Content</button>

</body>
</html>

上获取以下错误消息if(txt.test('hello')) Jscript错误:'undefined'为null或不是对象


编辑3 我猜我只是愚蠢,但我仍然无法让它工作,为什么我不能将xmlhttp.responseText存储到一个变量?

喜欢这个

<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url,cfunc)
{
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=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function myFunction()
{
loadXMLDoc("ajax_info.txt",function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
     var txt=xlmhttp.responseText;//This aint working, why, how can I store xlmhttp.responseText into a variable, that I can peform a search on?
    document.getElementById("myDiv").innerHTML=txt;//This aint working, why?????
    }
  });
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="myFunction()">Change Content</button>

</body>
</html>

我可以补充说,如果我替换以下

,上述实际上是有效的
 var txt=xlmhttp.responseText;
document.getElementById("myDiv").innerHTML=txt;

用这个

document.getElementById("myDiv").innerHTML=xlmhttp.responseText;

我没有得到回调函数,如下所述,我得到的是xmlhttp是未定义的,所以我问这个有效(至少是我想要它的一半)。

再次对不理解感到遗憾,但是必须有一些显而易见的事情,我不知道这个,这根本不可能将它存储在变量或其他东西中。

1 个答案:

答案 0 :(得分:0)

var txt=loadXMLDoc();

loadXMLDoc不会返回任何内容,因此txt在此之后为undefined。当然,undefined没有test方法,这是String.prototype的方法。

而是将回调处理程序分配给XMLHttpRequest,并在那里做任何你想做的事。

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4)
        if (xmlhttp.status==200) {
            // do something with xmlhttp.responseText
        } else {
            // do something appropriate with status
        }
}

更新:虽然我认为通常不喜欢这些复制面食示例,但我可以告诉你你应该把这段代码放在哪里。而不是这样做:

function searchADocument() { //wordToSearchFor
    var txt = loadXMLDoc();
    if (txt.test('hello'))
        alert('responseText contains "hello"');
    else
        document.getElementById("myDiv").innerHTML = "helloaj";
}

你在哪里测试loadXMLDoc的返回值(如前所述,它会立即返回,所以请求完成之前),你应该把你的代码放在回调处理程序中,您通过设置onreadystatechange分配:

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        var txt=xmlhttp.responseText; /* manipulate the DOM here */
        if (txt.test('hello'))
            alert('responseText contains "hello"');
        else
            document.getElementById("myDiv").innerHTML = "helloaj";
    }
}