关于xmlHttp的问题

s1120624175 2011-07-21 01:00:25
正在做一个获取网站源代码 搜索关键字的脚本
我会从主页抓取可用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("|");
}
...全文
349 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
s1120624175 2011-07-21
  • 打赏
  • 举报
回复
17楼正解
kyzy_yy_pm 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 s1120624175 的回复:]


我把”if(xmlHttp == null){“去掉了 这确实是问题 但不是根本的
因为之前本来就是局部变量
后来我试想 xmlHttp 只创建1次 看来不行
现在的情况是 成功访问几百次 返回请求的只有几次而已
[/Quote]
局部的?你的xmlHttp在那块定义的?
  • 打赏
  • 举报
回复
你是怎么访问的?并发,这样IE只会响应最近一次发送返回的请求

如并发同时发送2个请求,第一个没返回第二个就发送了,那么只执行第二次发送的回调,第一次的结果被覆盖了。


将全局变量改为局部变量。

这种蜘蛛类型的功能不是js的强项,改成其他语言。
s1120624175 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 kyzy_yy_pm 的回复:]

引用 5 楼 s1120624175 的回复:

引用 4 楼 kyzy_yy_pm 的回复:

xmlHttp 是在外部声明的吗?

xmlHttp是全局变量

这个不要用全局,你换成局部的试一试
[/Quote]
我把”if(xmlHttp == null){“去掉了 这确实是问题 但不是根本的
因为之前本来就是局部变量
后来我试想 xmlHttp 只创建1次 看来不行
现在的情况是 成功访问几百次 返回请求的只有几次而已
kyzy_yy_pm 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 confidenceyu 的回复:]

引用 11 楼 hch126163 的回复:

ajax 不能跨域

没有深刻的体会,可否举例说明
[/Quote]
你用ajax想别的网站请求下就知道了
confidenceyu 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hch126163 的回复:]

ajax 不能跨域
[/Quote]
没有深刻的体会,可否举例说明
kyzy_yy_pm 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 s1120624175 的回复:]

引用 4 楼 kyzy_yy_pm 的回复:

xmlHttp 是在外部声明的吗?

xmlHttp是全局变量
[/Quote]
这个不要用全局,你换成局部的试一试
s1120624175 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xs8577 的回复:]

JScript code
/* 响应 */ function returnHTTP(){
if (xmlHttp.readyState == 4) {
if(xmlHttp.status==200){ //加上这句试试
alert("Return");
getUrlArray(xmlHttp.responseText);
}
}
}
[/Quote]
这句都没有通过的话 if (xmlHttp.readyState == 4) {
if(xmlHttp.status==200){ //加上这句试试
不就没有意义了
hch126163 2011-07-21
  • 打赏
  • 举报
回复
ajax 不能跨域
xs8577 2011-07-21
  • 打赏
  • 举报
回复
/* 响应 */ function returnHTTP(){ 
if (xmlHttp.readyState == 4) {
if(xmlHttp.status==200){ //加上这句试试
alert("Return");
getUrlArray(xmlHttp.responseText);
}
}
}
s1120624175 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lsw645645645 的回复:]

引用楼主 s1120624175 的回复:
但是响应的很少很少

是什么意思?
[/Quote]
function sendHTTP
因为没有走catch部分 所以发送是正常的
function returnHTTP 是回调函数
响应少指的是没有执行alert("Return");
lsw645645645 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 s1120624175 的回复:]
但是响应的很少很少
[/Quote]
是什么意思?
s1120624175 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xuexiaodong2009 的回复:]

如果网页有图片之类的东西,不知道的多场时间,xmlHttp.responseText有什么?

if (xmlHttp.readyState == 4) {
alert("Return"); //响应很少
getUrlArray(xmlHttp.responseText);
}
[/Quote]
xmlHttp.responseText就是该网址的源代码 和右键查看出来的几乎一样
xuexiaodong2009 2011-07-21
  • 打赏
  • 举报
回复
如果网页有图片之类的东西,不知道的多场时间,xmlHttp.responseText有什么?

if (xmlHttp.readyState == 4) {
alert("Return"); //响应很少
getUrlArray(xmlHttp.responseText);
}
s1120624175 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kyzy_yy_pm 的回复:]

xmlHttp 是在外部声明的吗?
[/Quote]
xmlHttp是全局变量
kyzy_yy_pm 2011-07-21
  • 打赏
  • 举报
回复
xmlHttp 是在外部声明的吗?
s1120624175 2011-07-21
  • 打赏
  • 举报
回复

/* 建立 Http响应*/
function createXmlHttp() {

if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
xuexiaodong2009 2011-07-21
  • 打赏
  • 举报
回复
现成的ajax不用,自己写,给自己找事啊
s1120624175 2011-07-21
  • 打赏
  • 举报
回复
自己顶一下 补充说明
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)
就是好用的

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧