我使用类似于下面简化代码的代码在论坛网站上搜索几页帖子,然后在控制台中列出链接。
$(document).ready(function () {
for(i = 0; i <= 735; i += 15) {
var xhrs = new XMLHttpRequest();
xhrs.open("get", 'http://fakeURL.com/' + i, true);
xhrs.onreadystatechange = function()
{
if (this.readyState == 4)
{
$(this.responseText).find('a').each(function()
{
var url = $(this).attr('href');
console.log(url);
});
}
}
xhrs.send();
}
}
问题是有时候我会在控制台中收到这样的错误:
Resource interpreted as Image but transferred with MIME type text/html: "dailydawdle.com/".
Error: GET http://puu.sh/4Ueom.png 404 (Not Found) 4Ueom.png:1
Error: GET http://thisUrlIsTooLong.jpg 403 (Forbidden) cellphonebuyers large verge super wide.jpg:1
这些错误通常不会阻止代码完成网址列表,但有一次它会退出网页并转到Chrome中的红色屏幕,表示类似“安全错误”的内容。
即使我只搜索每个页面以查找以某个字符串开头的链接,我也会收到这些错误。我说即使我将find('a')
替换为find("a[href^='http://forum.posts/']")
以便只搜索论坛的链接,我仍会收到外部网站链接的错误(由论坛可能),有时还会出现一个红色警告页面,上面写着“安全错误”。