javascript将内容加载到div中错误:0

发布于 2025-01-02 00:04:47 字数 1131 浏览 1 评论 0原文

我正在使用 javascript 从 URL 中提取数据并将其放入 div 中:

function ahah(url, target) {
    document.getElementById(target).innerHTML = 'Fetching fixtures...';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("GET", url, true);
    req.send("");
    }
}  

function ahahDone(url, target) {
    if (req.readyState == 4) { // only if req is "loaded"
        if (req.status == 200) { // only if "OK"
            document.getElementById(target).innerHTML = req.responseText;
        } else {
            document.getElementById(target).innerHTML=" Error:\n"+ req.status + "\n" +req.statusText;
        }
    }
}

function load(name, div) {
    ahah(name,div);
return false;
}

load('http://www.domain.com/feeds/','content');

问题是,如果要加载的 URL 是本地的,则它可以正常工作 (/feeds/),但是当我将其更改为指向外部站点 (http ://www.domain.com/feeds/) 我打算保存文件的地方出现“错​​误 0”。

我已检查该 URL 是否有可用内容,但除非它是本地的,否则它将无法工作。非常欢迎任何反馈,谢谢。

I am using javascript to pull data from a URL and put it into a div:

function ahah(url, target) {
    document.getElementById(target).innerHTML = 'Fetching fixtures...';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("GET", url, true);
    req.send("");
    }
}  

function ahahDone(url, target) {
    if (req.readyState == 4) { // only if req is "loaded"
        if (req.status == 200) { // only if "OK"
            document.getElementById(target).innerHTML = req.responseText;
        } else {
            document.getElementById(target).innerHTML=" Error:\n"+ req.status + "\n" +req.statusText;
        }
    }
}

function load(name, div) {
    ahah(name,div);
return false;
}

load('http://www.domain.com/feeds/','content');

The problem is that if the URL to load is local it works fine (/feeds/), but when I change it to point to an external site (http://www.domain.com/feeds/) where I intend to hold the file I get an 'Error 0'.

I have checked the URL has the content available but unless it is local it will not work. Any feedback is very welcome, thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-残月青衣踏尘吟 2025-01-09 00:04:47

“错误”0 仅表示请求已加载到本地计算机上。没什么可担心的:)

只需更改这行代码以接受状态 0:

if (req.status == 200 || req.status == 0) {

编辑:正如一些评论者所说提到过,您也可能遇到域起源问题,尽管我不认为这是本例中的问题。

An 'error' of 0 simply means that the request loaded on the local machine. Nothing to worry about :)

Just change this line of code to accept a status of 0:

if (req.status == 200 || req.status == 0) {

Edit: As some commenters have mentioned, you could have domain-origin problems as well, though I don't believe that is the problem in this instance.

七度光 2025-01-09 00:04:47

我发现您尝试获取的 html 页面应该驻留在您的网站(域)上。您尝试从其他站点获取的 html 片段将不会被获取。

I found out that the html pages you are trying to fetch should reside on your website (domain). The html fragments you try to fetch from other sites wont be fetched.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文