How to “search” through the response of an XMLHttp

2019-09-03 08:19发布

I have a question regarding the followng code

<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>

Now I want to search through the xmlhttp.responseText (in other words call the function loadXMLDoc()) for key words, like for example "testfile" and if it exists multiple example "testfile_1" and "testfile_2"....."testfile_n" then "doSomething"

like this

 <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>

I don't know where to start since I don't know what type xmlhttp.responseText is, can I store it in an array and scan it using for loop etc? Thanks in advance. =)

EDIT What am Im doing wrong here

<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>

get the following error message on if(txt.test('hello')) Jscript error:'undefined' is null or not an object


EDIT 3 Im guessing im just dumb as hell, but I still can't get this to work, why can't I store the xmlhttp.responseText into a variable?

Like this

<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>

I can add that the above actually works if I replace the following

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

with this

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

I haven't got the call back function to work as mentiond below, all I get is that xmlhttp is undefined, so I ask on this that works(at least half the way I want it to).

Again sorry for not understanding, but there must be something obvious that I don't get about this, that this simply isn't possible to store this in a variable or something.

1条回答
萌系小妹纸
2楼-- · 2019-09-03 08:57
var txt=loadXMLDoc();

loadXMLDoc doesn't return anything, so txt is undefined after this. Of course undefined doesn't have the test method, which is a method of String.prototype.

Instead assign a callback handler to your XMLHttpRequest and do whatever you want there.

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

Update: Though I think it's usually not preferrable to have these kind of copy-pasta examples, I can show you where you should put this piece of code. Instead of doing this:

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

where you test the return value of loadXMLDoc (which, as already stated, returns immediately, so before the request is completed), you should put your code inside the callback handler, that you assign by setting 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";
    }
}
查看更多
登录 后发表回答