如何“搜索”通过XMLHttpRequest的响应 [英] How to "search" through the response of an XMLHttpRequest

查看:72
本文介绍了如何“搜索”通过XMLHttpRequest的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于followng代码的问题

 < html> 
< head>
< script type =text / javascript>
函数loadXMLDoc()
{
var xmlhttp;
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为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>让AJAX更改此文字< / h2>< / div>
< button type =buttononclick =loadXMLDoc()>更改内容< / button>

< / body>
< / html>

现在我要搜索xmlhttp.responseText(换句话说,调用函数 loadXMLDoc ()),例如testfile,如果它存在多个示例testfile_1和testfile_2.....testfile_n,则doSomething

像这样

 < html> 
< head>
< script type =text / javascript>
函数loadXMLDoc()
{
var xmlhttp;
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4&& xmlhttp.status == 200)
{
return xmlhttp.responseText;





$ b函数searchADocument(wordToSearchFor){
xmlhttp.open(GET, index.html的,真正的);
xmlhttp.send();
int numberOfTimesWordOccurs = 0;
var thePageToSearchThrough [] = loadXMLDoc();
for(i = 0; i< thePageToSearchThrough.length; i ++){
if(thePageToSearchThrough [i] == wordToSearchFor)
numberOfTimesWordOccurs ++;
}
如果(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>



其他

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

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

< div id =myDiv>< h2>让AJAX更改此文字< / h2>< / div>
< button type =buttononclick =searchADocument(testfile)>更改内容< / button>

< / body>
< / html>

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



编辑
我在这里做错了什么

 < HTML> 
< head>
< script type =text / javascript>
函数loadXMLDoc()
{
var xmlhttp;
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4&& xmlhttp.status == 200)
{

返回xmlhttp.responseText;
}
}
xmlhttp.open(GET,index.html,true);
xmlhttp.send();


函数searchADocument(){// wordToSearchFor
var txt = loadXMLDoc();
if(txt.test('hello'))alert('responseText containshello');
else {
document.getElementById(myDiv)。innerHTML =helloaj;
}

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

< div id =myDiv>< h2>让AJAX更改此文字< / h2>< / div>
< button type =buttononclick =searchADocument()>更改内容< / button>

< / body>
< / html>

if(txt.test('hello')) Jscript错误:'undefined'为空或不是对象




编辑3
我猜我即使是愚蠢的地狱,但我仍然无法得到这个工作,为什么我不能将xmlhttp.responseText存储到变量?



像这样

 < html> 
< head>
< script type =text / javascript>
var xmlhttp;
函数loadXMLDoc(url,cfunc)
{
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp =新的XMLHttpRequest();
}
else
{//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange = cfunc;
xmlhttp.open(GET,url,true);
xmlhttp.send();

函数myFunction()
{
loadXMLDoc(ajax_info.txt,function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var txt = xlmhttp.responseText; //这不工作,为什么,我如何将xlmhttp.responseText存储到一个变量中,以便我可以执行搜索?
document.getElementById(myDiv)。innerHTML = txt; // This is a working,why ?????
}
});
}
< / script>
< / head>
< body>

< div id =myDiv>< h2>让AJAX更改此文字< / h2>< / div>
< button type =buttononclick =myFunction()>更改内容< /按钮>

< / body>
< / html>

我可以补充说,以上实际工作如果我取代以下

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

用这个

 的document.getElementById( myDiv)的innerHTML = xlmhttp.responseText。 

我得到的回调函数没有如下所述,我得到的是xmlhttp是不确定的,所以我问这个问题是否有效(至少是我希望的一半)。

再次抱歉不理解,但必须有一些明显的东西,我不明白这一点,这是不可能的,将它存储在一个变量或。

解决方案

  var txt = loadXMLDoc(); 

loadXMLDoc 不会返回任何内容,所以 txt 之后是 undefined 。当然, undefined 没有 test 方法,这是 String方法。原型



改为给你的 XMLHttpRequest 分配一个回调处理程序,然后做任何你想要的

  xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4)
if(xmlhttp.status == 200){
//用xmlhttp.responseText
做些事情} else {
//用状态
做适当的事情}
}

更新:虽然我认为通常不会有这些类型的复制面食的例子,我可以告诉你你应该把这段代码放在哪里。而不是这样做:
$ b $ pre $ 函数searchADocument(){// wordToSearchFor
var txt = loadXMLDoc();
if(txt.test('hello'))
alert('responseText containshello');
else
document.getElementById(myDiv)。innerHTML =helloaj;
}

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

  xmlhttp.onreadystatechange = function(){
if(xmlhttp。 readyState == 4&& xmlhttp.status == 200){
var txt = xmlhttp.responseText; / *在这里操作DOM * /
if(txt.test('hello'))
alert('responseText containshello');
else
document.getElementById(myDiv)。innerHTML =helloaj;
}
}


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.

解决方案

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";
    }
}

这篇关于如何“搜索”通过XMLHttpRequest的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆