火狐和放大器;文档元素之后AJAX垃圾 [英] Firefox & AJAX Junk after document element

查看:131
本文介绍了火狐和放大器;文档元素之后AJAX垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用的网页抓取脚本动态地加载网页变成一个div。继承人的code。 BTW即时通讯使用的是Firefox W / Kubuntu的

 函数取(URL,DIVID){
        REQ = window.XMLHtt prequest?新XMLHtt prequest():新的ActiveXObject(MSXML2.XMLHTTP.3.0);
    req.open(GET,URL);
    req.onreadystatechange =功能(){
        如果(req.readyState == 4和&安培; req.status == 200){
            的document.getElementById(DIVID).innerHTML = req.responseText;
        }
    }
    req.send(空);
}
 

当我设法得到它加载一个页面没有任何反应,我得到一个错误

  

错误:文档元素后的垃圾
  源文件:文件:///home/amnite/Stuff/MetalBraska/Shows/ContentRight.html
  行:2列:1
  来源$ C ​​$ C:
  < IMG SRC =布局/ HeaderDiv.jpgWIDTH =250像素高度=7px的>

解决方案

我的答案是在第

  • 错误
  • 但是,这并不甚至不管
  • 在真正的问题
  • 怎么有人能找到病因/真正的问题
  • 系统解决方案,包括错误处理

错误

这个错误是因为它试图解析 ContentRight.html 为XML文件。 XML文件是严格的,因此任何小东西就像漏了< / IMG> 将导致整体的失败。在这种情况下,有一个以上的顶层元素。 (在一个普通的HTML,只有一个顶级元素< HTML> )。第二个顶级元素被认为是垃圾文档元素后。我假设图像是导致问题的第二个元素。

但是,这甚至不事

不过,这一切都是跑题了。没有理由为你解析XML中的第一页。你只需要在HTML。对?我的猜测是,它试图解析XML,并将其存储到 responseXML的。由于错误的, resonseXML 为空。但是,你正在使用的responseText 所以应该没有问题。 (忽略错误)

真正的问题

所有这一切,现在我明白了这个问题。您需要HTTP状态200 req.status == 200 。既然你不使用的http:// 键,您使用的是文件,而不是:// ,没有状态code和服务器错误 500 和检测未发现 404 很少有机会的可能性。因此,所有你将得到的是 0 。当你得到这样一个好的做法是添加报警线

怎么有人能找到病因/真正的问题

  req.onreadystatechange =功能(){
    警报(req.readyState)
    如果(req.readyState == 4和&安培; req.status == 200){
        的document.getElementById(DIVID).innerHTML = req.responseText;
    }
}
 

会提醒 1 2 3 4 ,所以你知道的readyState 是4。然后尝试

  req.onreadystatechange =功能(){
    如果(req.readyState == 4)
        警报(req.status)
    如果(req.readyState == 4和&安培; req.status == 200){
        的document.getElementById(DIVID).innerHTML = req.responseText;
    }
}
 

您将获得 0 键,你会更接近问题。

一个解决方案,包括错误处理

一个好的解决方案是

  req.onreadystatechange =功能(){
    如果(req.readyState == 4和及(req.status == 200 || req.status == 0&安培;&安培; req.responseText)){
        的document.getElementById(DIVID).innerHTML = req.responseText;
    } 其他 {
        的document.getElementById(DIVID).innerHTML ='< B>错误。 HTTP'+ req.status +'< / B>
    }
}
 

因为如果状态为0,即的意思是网络连接故障在这种情况下的responseText 将空白的,然后你会得到错误。 HTTP 0 。如果不是空白的,这意味着它是文件:// 和它的工作就像一个魅力

当然,服务器级别的错误会给错误。 HTTP 500 或诸如此类的东西。

Im using a page fetch script to dynamically load a web page into a div. Heres the code. BTW Im using Firefox w/ Kubuntu

function fetch(URL, divId) {
        req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
    req.open("GET", URL);
    req.onreadystatechange = function() {
        if (req.readyState == 4 && req.status == 200) {
            document.getElementById(divId).innerHTML = req.responseText;
        }
    }
    req.send(null);
}

When i try to get it to load a page nothing happens and I get an error

Error: junk after document element
Source File: file:///home/amnite/Stuff/MetalBraska/Shows/ContentRight.html
Line: 2, Column: 1
Source Code:
<img src="Layout/HeaderDiv.jpg" width="250px" height="7px">

解决方案

My answer is in sections

  • The error
  • But that does not even matter
  • The real problem
  • How someone could have found the cause/real problem
  • A solution that includes error handling

The error

The error is because it is trying to parse ContentRight.html as an XML file. XML files are strict, so any little thing like a missing </img> would cause a whole failure. In this case, there is more than one top level element. (in a normal HTML, there is only one top level element <html>). The second top level element is considered "Junk after document element". I am assuming that image is the second element that is causing the problem.

But that does not even matter

But this is all beside the point. There is no reason for you to parse the XML in the first page. You just want the HTML. Right? My guess is, it is trying to parse the XML and store it to responseXML. Because of the error, resonseXML is null. But you are using responseText so there should be no problem. (ignore the error)

The real problem

All that and now I see the problem. You are requiring HTTP status 200 with req.status == 200. Since you are not using http:// and you are instead using file://, there is no status code and no possibility of a server error 500 and little chance of detecting a not found 404. So all you will get is 0. A good practice when you get something like this is to add alert lines

How someone could have found the cause/real problem

req.onreadystatechange = function() {
    alert(req.readyState)
    if (req.readyState == 4 && req.status == 200) {
        document.getElementById(divId).innerHTML = req.responseText;
    }
}

would alert 1 2 3 4 so you know readyState is 4. Then try

req.onreadystatechange = function() {
    if(req.readyState == 4)
        alert(req.status)
    if (req.readyState == 4 && req.status == 200) {
        document.getElementById(divId).innerHTML = req.responseText;
    }
}

You would get 0 and you would be closer to the problem.

A solution that includes error handling

A good solution is

req.onreadystatechange = function() {
    if (req.readyState == 4 && (req.status == 200 || req.status == 0 && req.responseText)) {
        document.getElementById(divId).innerHTML = req.responseText;
    } else {
        document.getElementById(divId).innerHTML = '<b>Error. HTTP ' + req.status + '</b>'
    }
}

Because if status is 0, that could mean internet connection failure in which case responseText would be blank and then you would get Error. HTTP 0. If it was not blank, that would mean it was file:// and it would work like a charm.

And of course a server-level error would give Error. HTTP 500 or whatnot.

这篇关于火狐和放大器;文档元素之后AJAX垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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