正在做一个获取网站源代码 搜索关键字的脚本
我会从主页抓取可用URL 一层层向下访问
但是现在遇到这样的问题 问题 1
发送请求都是成功的 但是响应的很少很少
这是为什么?    /* 发送请求 */
    function sendHTTP(url){
        try{
            if(xmlHttp == null){
                createXmlHttp();    
            }                                        
            document.getElementById("sourceUrl").value = url;
            //alert("Send:"+url);
            xmlHttp.onreadystatechange = returnHTTP;                  
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
        }catch(e){
            alert("SEND ERROR:" + url);
            document.getElementById("sourceUrl").value = "SEND ERROR:" + url;
        }
    }
    
    /* 响应 */
    function returnHTTP(){
        if (xmlHttp.readyState == 4) {
            alert("Return"); //响应很少
            getUrlArray(xmlHttp.responseText);
        }
    }
问题2    /* 将源代分解成URL和关键字 */
    function getUrlArray(str){
    
        searching(str);
        //var ary = str.replace(/http:\/\//g,"\nhttp://").match(/http:\/\/[^\"'{\*;]+\.html/g);//这一行是有效的 只有扩展名写死 html
        var ary = str.replace(/http:\/\//g,"\nhttp://").match(eval("/http:\/\/[^\"'{\*;]+\.("+ getExtension() +")/g"));//这一行会抛错  Message: Syntax error 不知道原因
......
......
......
    /* 扩展名限制*/
    function getExtension(){
        var ary = new Array("html","shtml","htm","com");
        return ary.join("|");
    }

解决方案 »

  1.   

    自己顶一下 补充说明
    var ary = str.replace(/http:\/\//g,"\nhttp://").match(/http:\/\/[^\"'{\*;]+\.html/g);
    var ary = str.replace(/http:\/\//g,"\nhttp://").match(eval("/http:\/\/[^\"'{\*;]+\.("+ getExtension() +")/g"));
    两个正则不同在于 match部分的  "html"  → getExtension() 
    如果换成这么写
    .match(/http:\/\/[^\"'{\*;]+\.(html|shtml|htm|com)/g)
    就是好用的
      

  2.   

    现成的ajax不用,自己写,给自己找事啊
      

  3.   


        /* 建立 Http响应*/
        function createXmlHttp() {
        
            if (window.XMLHttpRequest) {
               xmlHttp = new XMLHttpRequest();                 
            } else {
               xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
      

  4.   

    xmlHttp 是在外部声明的吗?
      

  5.   

    如果网页有图片之类的东西,不知道的多场时间,xmlHttp.responseText有什么?  if (xmlHttp.readyState == 4) {
                alert("Return"); //响应很少
                getUrlArray(xmlHttp.responseText);
            }
      

  6.   

    xmlHttp.responseText就是该网址的源代码  和右键查看出来的几乎一样
      

  7.   

    function sendHTTP 
    因为没有走catch部分 所以发送是正常的
    function returnHTTP 是回调函数 
    响应少指的是没有执行alert("Return");
      

  8.   

    /* 响应 */ function returnHTTP(){ 
    if (xmlHttp.readyState == 4) { 
    if(xmlHttp.status==200){   //加上这句试试
    alert("Return"); 
    getUrlArray(xmlHttp.responseText); 
    }

    }
     
      

  9.   

    这句都没有通过的话 if (xmlHttp.readyState == 4) { 
    if(xmlHttp.status==200){   //加上这句试试
    不就没有意义了
      

  10.   

    你用ajax想别的网站请求下就知道了
      

  11.   

    我把”if(xmlHttp == null){“去掉了  这确实是问题 但不是根本的
    因为之前本来就是局部变量
    后来我试想 xmlHttp 只创建1次 看来不行
    现在的情况是 成功访问几百次  返回请求的只有几次而已
      

  12.   

    你是怎么访问的?并发,这样IE只会响应最近一次发送返回的请求如并发同时发送2个请求,第一个没返回第二个就发送了,那么只执行第二次发送的回调,第一次的结果被覆盖了。
    将全局变量改为局部变量。这种蜘蛛类型的功能不是js的强项,改成其他语言。
      

  13.   

    局部的?你的xmlHttp在那块定义的?