javascript将内容加载到div中错误: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“错误”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.
我发现您尝试获取的 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.